Skip to content Skip to sidebar Skip to footer

How Do I Set A Time Limit To My Splash Screen?

I am coding in Eclipse for an Android App. I have developed a splash screen which I need to display for 5 seconds before my app starts. How to do it?

Solution 1:

       Thread timer=new Thread()
        {
            publicvoidrun() {
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally
                {
                    Intent i=new Intent(SplashScreen.this,MainActivity.class);
                    finish();
                    startActivity(i);
                }
            }
        };
        timer.start();

Solution 2:

Use the Async Class to perform the sleep operation in the doinbackground function and in the post function do the rest of the task

publicclassSplashScreenActivityextendsActivity {
        privatefinalintSPLASH_DISPLAY_LENGHT=5000;
        @OverridepublicvoidonCreate(Bundle icicle)
            {        super.onCreate(icicle);

            try{            
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.activity_splashscreen);
            }catch(Exception e){
                e.printStackTrace();
            }
             newMyAsyncTask().execute();   
            }

         privateclassMyAsyncTaskextendsAsyncTask<Void, Void, Void>{
              @OverrideprotectedvoidonPreExecute(){
                    // show your progress dialog
              }
              @Overrideprotected Void doInBackground(Void... voids){

                  try {
                        Thread.sleep(SPLASH_DISPLAY_LENGHT);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                returnnull;
              }

              @OverrideprotectedvoidonPostExecute(Void params)
              {


                   startActivity(newIntent(SplashScreenActivity.this, HomeActivity.class));

                    finish();
              }

           }



    }

Solution 3:

use like that

publicclassSplaceScreenActivityextendsActivity {

    privatestaticfinalintSPLASH_DISPLAY_TIME=2500;

    // SplashScreen Splash;@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splacescreen);

        newHandler().postDelayed(newRunnable() {
            publicvoidrun() {

                Intentintent=newIntent();
                intent.setClass(SplaceScreenActivity.this,
                        HomeScreenActivity.class);

                SplaceScreenActivity.this.startActivity(intent);
                SplaceScreenActivity.this.finish();

            }
        }, SPLASH_DISPLAY_TIME);
    }
}

Solution 4:

Try below code:

publicclassMainActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);

        finalintwelcomeScreenDisplay=2000;
        /** create a thread to show splash up to splash time */ThreadwelcomeThread=newThread() {
            intwait=0;

            @Overridepublicvoidrun() {
                try {
                    super.run();
                    /**
                     * use while to get the splash time. Use sleep() to increase
                     * the wait variable for every 100L.
                     */while (wait < welcomeScreenDisplay) {
                        sleep(100);
                        wait += 100;
                    }
                } catch (Exception e) {

                } finally {
                    startActivity(newIntent(MainActivity.this,
                            HomeActivity.class));
                    finish();
                }
            }
        };
        welcomeThread.start();

    }

}

Solution 5:

  Handler handler = new Handler();

        Runnable run = new Runnable() {

            publicvoidrun() {
                // TODO Auto-generated method stub
                startActivity(new Intent(SplaceActivity.this, New.class));
                overridePendingTransition(0, 0);
                finish();
            }
        };

        handler.postDelayed(run, 3000);   

Post a Comment for "How Do I Set A Time Limit To My Splash Screen?"