Jade Jose
Jade Jose

Reputation: 11

Launch an Activity after timer

So I have this code, that has a countdown timer in a service with a 10 second timer. What I want to do is in the onFinish() method I want to launch the Activity (which is called MainActivity) automatically even when I am outside the app.

public class TimeDisplayTimerTask extends TimerTask{
  CountDownTimer timer;

  NotificationCompat.Builder notification;
  private static final String TAG="com.timer";
  private Handler mHandler = new Handler();

  @Override
  public void run() {

    // run on another thread
    mHandler.post(new Runnable() {

      @Override
      public void run() {

          // display toast
         timer = new CountDownTimer(10000, 1000) {
           @Override
           public void onFinish(){

           }

           @Override
           public void onTick(long millisUntilFinished) {
             Log.i(TAG,"" + millisUntilFinished/1000);

           }
         };
         timer.start();
      }

    });

  }
}

Upvotes: 1

Views: 265

Answers (4)

Cedric Franck
Cedric Franck

Reputation: 526

You neet to pass an activity context to your TimeDisplayTimerTask :

public class TimeDisplayTimerTask extends TimerTask{
 CountDownTimer timer;

 NotificationCompat.Builder notification;
 private static final String TAG="com.timer";
 private Handler mHandler = new Handler();
private Activity mActivity;

public TimeDisplayTimerTask(Activity activity){
    mActivity = activity;
    super();
}

@Override
public void run() {

    // run on another thread
    mHandler.post(new Runnable() {

        @Override
        public void run() {

        // display toast
         timer = new CountDownTimer(10000, 1000) {
            @Override
            public void onFinish(){
                  if (activity != null {
                      Intent startIntent = new Intent(activity,  MainActivity.class);
                      startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
                      activity.startActivity(startIntent);
                  }    
            }

            @Override
            public void onTick(long millisUntilFinished) {
                Log.i(TAG,"" + millisUntilFinished/1000);

            }

         };
         timer.start();
    }

});

}
}

Upvotes: 0

Ashwani Kumar
Ashwani Kumar

Reputation: 1472

If you are inside a service then simply launch you activity using intent. Put this code in your onFinish() method:

Intent i = new Intent();
i.setClass(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Upvotes: 0

Dixit Panchal
Dixit Panchal

Reputation: 3436

Try This to open your main activty after 10 second.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           // launch your main activity here and finish your current activity
        }
    }, 10000);
}

Upvotes: 1

Mjafko
Mjafko

Reputation: 161

Probably you will have to override lifecycle method's and also use PARTIAL_WAKE_LOCK to keep cpu running untill you finish execution, in case user lock screen.

Upvotes: 0

Related Questions