Question

how to manage a countdown to begin right away with an activity start and to start new activity when time is up? Thanks in advance

Edits: Activity contatins a button. User should press it until time is up, otherwise another activity starts

Was it helpful?

Solution 2

You can achieve this in two ways

METHOD 1: Creation of a thread and setting time to sleep exactly after that redirection to main app screen. METHOD 2: Set time to handler and call Handler().postDelayed , it will surly call run method of runnable after setting the time and redirection to main app screen.

we can implement in this given way wher we can write code for our MainScreen

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainscreen);

// METHOD 1     

         /****** Create Thread that will sleep for 10 seconds *************/        
        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 10 seconds
                    sleep(10*1000);

                    // After 10 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),AnotherScreen .class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

//METHOD 2  

        /*
        new Handler().postDelayed(new Runnable() {

            // Using handler with postDelayed called runnable run method

            @Override
            public void run() {
                Intent i = new Intent(MainScreen.this, AnotherScreen.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, 10*1000); // wait for 10 seconds
        */
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

    }}

the AnotherScreen code can be as follows

    import android.app.Activity;
import android.os.Bundle;

public class AnotherScreen extends Activity {

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

    } 

    @Override
    protected void onDestroy() {

        super.onDestroy();

    }
}

for an another example visit http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/

https://codereview.stackexchange.com/questions/31295/review-request-android-countdowntimer-activity

OTHER TIPS

try this code

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
        //start new activity
     }
  }.start();

This is the answer you are looking for a spalshActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    try {
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this,
                        MainActivity.class);
                startActivity(i);
                finish();

            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 300);// DElay of 300 mil sec
    } catch (Exception e) {

        Log.e("ERROR in Splash Screen", e.toString());
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top