简体   繁体   中英

How to implement thread concept in Android application?

I have implemented one basic android applicaiton.In which I want to using thread concept.

When application will open then one splash screen display for some time (3 seconds) and then open new view.

I know how to open next view but I dont know how to display splash screen for 3 seconds.

Pleas give me idea about that.

Thanks in advance.

The CountDownTimer will be the good idea!

  public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.mylayout);

    lTimer = new CountDownTimer(3000, 1000) {

        public void onFinish() {
        closeScreen();
        }
        @Override
        public void onTick(long millisUntilFinished) {
        }
    }.start();
 }

 private void closeScreen() {
        Intent lIntent = new Intent();
        lIntent.setClass(getApplicationContext(), MainActivity.class);
            startActivity(lIntent);
            finish();
        }
      }

I'm not gonna come up with any code regarding this issue as it is way more important to make you understand what a splash screen is used for.

A splash screen is used when your application needs to perform some kind of initialization process which takes some time for example your application depends on network data which it has to fetch and parse. It is there to present something to the user while he is waiting. In no case a splash screen should be used without a good reason that means just displaying a splash screen for a certain time without doing anything important in the background.

I think you are in search of this:

code for Splash_Activity

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

    // thread for displaying the SplashScreen
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;


       while(_active && (waited < 3000)) {
                sleep(100);
                if(_active) {
                    waited += 100;
                }
            }
        } catch(InterruptedException e) {
            // do nothing
        } finally {
            finish();
            Intent intent = new Intent(getBaseContext(), nextActivity.class);
            startActivity(intent);
            stop();
        }
    }
};
splashTread.start();
}

Here is also a good example : http://www.androidpeople.com/android-loading-welcome-splash-spash-screen-example/

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

                Intent in = new Intent(SplashActivity.this,NextActivity.class);
                startActivity(in);
                    finish();
            }
        },3000);

You can use threads just as you use them in plain old normal Java apps :). Try to google , there are a couple of good examples out there. Here's one I just found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM