Splashscreen - 第一次使用共享首选项

Ahm*_*mad 2 android splash-screen

你好我想在默认情况下显示另一个启动画面,如果应用程序第一次启动(安装后如右)

所以我写了这个.但是新的Activity没有启动它停留在Splash屏幕上.有人可以说它有什么不对吗?

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;

    public class splash extends Activity {
         private Thread splashTread;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            if(!prefs.getBoolean("firstTime", false)) {
                // run your one time code
                 Intent i = new Intent(splash.this, main.class);

                         startActivity(i);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("firstTime", true);
                editor.commit();


             // thread for displaying the SplashScreen
             splashTread = new Thread() {
                 @Override
                 public void run() {
                     try {
                         synchronized(this){

                                 //wait 2 sec
                                 wait(2000);
                         }

                     } catch(InterruptedException e) {}
                     finally {
                         finish();



                         //start a new activity
                         Intent i = new Intent();
                         i.setClass(splash.this, main.class);
                                 startActivity(i);

                         stop();
                     }
                 }
             };

             splashTread.start();

        }

    }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢.

Xon*_*ono 6

从我所看到的,无论是否第一次启动,您的代码都运行相同的Activity(main).我假设您的目的是在第一次启动时立即启动备用启动屏幕,否则在2秒后继续执行主活动.另外,我建议使用Handler而不是Thread,因为你只使用它一次,并且延迟使用.试试这个:

    public class splash extends Activity
{
    private Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            Intent i = new Intent(splash.this, main.class);
            splash.this.startActivity(i);
                                 this.finish()
        }
    };

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        if(!prefs.getBoolean("first_time", false))
        {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("first_time", true);
            editor.commit();
            Intent i = new Intent(splash.this, otherSplash.class);
            this.startActivity(i);
                                 this.finish();
        }
        else
        {
            this.setContentView(R.layout.splash);
            handler.sendEmptyMessageDelayed(0, 2000);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)