This question already has answers here:
Android SplashScreen
                                
                                    (4个答案)
                                
                        
                                4年前关闭。
            
                    
我可以找到两种方法来实现splash screen

public class MainSplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_splash_screen);

// METHOD 1

         /****** Create Thread that will sleep for 5 seconds *************/
        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),MainActivity.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

//METHOD 2

        /*
        new Handler().postDelayed(new Runnable() {

            // Using handler with postDelayed called runnable run method

            @Override
            public void run() {
                Intent i = new Intent(MainSplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, 5*1000); // wait for 5 seconds
        */
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

    }
}


正如预期的那样,这两种方法都首先显示启动屏幕5秒钟,然后启动MainActivity。

在我的应用程序中,MainActivity启动相对耗时。因此,在这两种方法中,我看到的是启动屏幕显示5秒钟,然后黑屏显示几秒钟(例如当我不使用启动屏幕时),然后是MainActivity。

问题是在启动屏幕显示期间如何启动MainActivity。

我试图定义两个线程并同时运行,但结果与不使用启动画面时的情况完全相同。

        Thread background = new Thread() {
            public void run() {
                try {
                    sleep(5*1000);
                    finish();
                } catch (Exception e) {}
            }
        };
        Thread mainTh = new Thread() {
            public void run() {
                try {
                    Intent i=new Intent(getBaseContext(),MainActivity.class);
                    startActivity(i);
                } catch (Exception e) {}
            }
        };
        mainTh.start();
        background.start();

最佳答案

尝试这个,

public class Splash extends Activity {

    private static final int SPLASH_SHOW_TIME = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      Fabric.with(this, new Crashlytics());
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        new BackgroundSplashTask().execute();

    }

    private class BackgroundSplashTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                Thread.sleep(SPLASH_SHOW_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Intent i = new Intent(Splash.this, MainActivity.class);
            startActivity(i);
            finish();
        }

    }
}

关于android - 在展示SplashScreen的过程中如何运行MainActivity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32777938/

10-12 00:17
查看更多