Best way to have a splash screen in an Android application?

后端 未结 6 1309
别跟我提以往
别跟我提以往 2020-12-17 00:40

I need a splash screen for my application. Tried creating an activity having the image for my splash screen; and tried using for loop and the Timer class for introducing a t

6条回答
  •  情深已故
    2020-12-17 01:30

    The simplest way I do for my every project is looks like this:

    public class SplashActivity extends Activity {
        protected boolean active = true;
        protected int splashTime = 1000;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash_screen);
            Thread splashTread = new Thread() {
                @Override
                public void run() {
                    try {
                        int waited = 0;
                        while(active && (waited < splashTime)) {
                            sleep(100);
                            if(active) {
                                waited += 100;
                            }
                        }
                    } catch(InterruptedException e) {
                        // do nothing
                    } finally {
                        finish();
                        // Start your Activity here
                   }
               }
           };
           splashTread.start();    
       }
     //...
    

提交回复
热议问题