How do I set a time limit to my splash screen? [closed]

梦想的初衷 提交于 2019-12-11 19:41:56

问题


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?


回答1:


       Thread timer=new Thread()
        {
            public void run() {
                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();



回答2:


use like that

public class SplaceScreenActivity extends Activity {

    private static final int SPLASH_DISPLAY_TIME = 2500;

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

        new Handler().postDelayed(new Runnable() {
            public void run() {

                Intent intent = new Intent();
                intent.setClass(SplaceScreenActivity.this,
                        HomeScreenActivity.class);

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

            }
        }, SPLASH_DISPLAY_TIME);
    }
}



回答3:


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

public class SplashScreenActivity extends Activity {
        private final int SPLASH_DISPLAY_LENGHT = 5000;
        @Override
            public void onCreate(Bundle icicle)
            {        super.onCreate(icicle);

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

         private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
              @Override
              protected void onPreExecute(){
                    // show your progress dialog
              }
              @Override
              protected Void doInBackground(Void... voids){

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

              @Override
              protected void onPostExecute(Void params)
              {


                   startActivity(new Intent(SplashScreenActivity.this, HomeActivity.class));

                    finish();
              }

           }



    }



回答4:


Try below code:

public class MainActivity extends Activity {

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

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

        final int welcomeScreenDisplay = 2000;
        /** create a thread to show splash up to splash time */
        Thread welcomeThread = new Thread() {
            int wait = 0;

            @Override
            public void run() {
                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(new Intent(MainActivity.this,
                            HomeActivity.class));
                    finish();
                }
            }
        };
        welcomeThread.start();

    }

}



回答5:


  Handler handler = new Handler();

        Runnable run = new Runnable() {

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

        handler.postDelayed(run, 3000);   



回答6:


Use AsyncTask or thread for this purpose.

http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/

hope it helps




回答7:


I use Timer:

Timer timer = new Timer();
    timer.schedule(new TimerTask(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent home_page = new Intent(Splash.class,HomePage.class);
            startActivity(home_page);
            finish();
        }}, 5000);



回答8:


Add these few lines of code in your splashActivity and it will start your second activity after 5seconds.

new Handler().postDelayed(new Runnable(){
                public void run() {
                    Intent mainIntent = new Intent(splashScreen.this,MainActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    splashScreen.this.startActivity(mainIntent);

                    splashScreen.this.finish();
                }
            }, 5000); 

PostDelayed Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.




回答9:


you can use Sleep method like this in your Splash Activity onCreate method:

        Thread timer1 = new Thread(){
        @Override
        public void run(){
            try{
                sleep(4000);
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
            finally{
                Intent intent = new Intent(SplashActivity.this, NextActivity.class);
                startActivity(intent);
            }


        }
    };
    timer1.start();

this take 4 sec to load NextActivity.




回答10:


Use handler

public class SplashActivity extends Activity {

int secondsDelayed = 5;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash);


    Message msg = new Message();
    msg.what = 0;
    mHandler.sendMessage(msg);
}


Handler mHandler = new Handler()
{
 public void handleMessage(android.os.Message msg) {

        switch(msg.what)
        {
        case 1:
        {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
        }
        break;
        case 0:
        {
            Message ms = new Message();
            ms.what = 1;
            mHandler.sendMessageDelayed(ms,  secondsDelayed * 1000);
        }
        break;
        }

    };
};



protected void onDestroy() {

    super.onDestroy();
    mHandler.removeMessages(1);
};
}

Note:Donot make Splash screen for 5 sec user will get irritated make it for 2sec



来源:https://stackoverflow.com/questions/19491073/how-do-i-set-a-time-limit-to-my-splash-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!