Since you also want to show the countdown, I would recommend CountDownTimer . This has methods to act on every tick, which may be the interval that you specified in the constructor. And its methods run on UI Thread, so you can easily update TextView, etc.
In this method, onFinish()you can call finish()for your own Activityor perform any other appropriate action.
See this answer for an example.
Edit with a clearer example
Here I have an inner class that extends CountDownTimer
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.some_xml);
timer = new MyCountDown(11000, 1000);
}
private class MyCountDown extends CountDownTimer
{
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
frameAnimation.start();
start();
}
@Override
public void onFinish() {
secs = 10;
startActivity(intent);
YourActivity.this.finish();
}
@Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
source
share