Skip to content Skip to sidebar Skip to footer

How To Start An Activity Upon The Completion Of A Timer?

I'm trying to start a new activity 'SMS.java', if I dont respond to my timer within 30secs. After 30secs, the new ativity should be started. Can anyone help me out??? The class Tim

Solution 1:

For a timer method, better you can use with threading. It will work. This is the example for Show Timer in android using threads. It runs the thread every second. Change the time if you want.

Timer MyTimer=new Timer();
        MyTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        TimerBtn=(Button) findViewById(R.id.Timer);
                        TimerBtn.setText(new Date().toString());
                }
                });
            }
            }, 0, 1000);

Solution 2:

What I do is call a method from the Activity on my CountDownTimer class, like this:

//Timer Class inside my Activity
    public class Splash extends CountDownTimer{

        public Splash(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            nextActivity(Login.class, true);
        }

        @Override
        public void onTick(long millisUntilFinished) {}
    }

//Method on my Activity Class

    protected void nextActivity(Class<?> myClass, boolean finish) {
        Intent myIntent = new Intent(this, myClass);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(myIntent);

        if(finish)
            finish();
    }

Post a Comment for "How To Start An Activity Upon The Completion Of A Timer?"