ManthanDalal
ManthanDalal

Reputation: 1073

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.

Upvotes: 1

Views: 2403

Answers (5)

prateek
prateek

Reputation: 508

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

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

Upvotes: 1

Octavian Helm
Octavian Helm

Reputation: 39604

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.

Upvotes: 3

Paresh Mayani
Paresh Mayani

Reputation: 128428

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/

Upvotes: 2

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40503

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();
        }
      }

Upvotes: 4

Juri
Juri

Reputation: 32900

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

Upvotes: 1

Related Questions