How do I set a completion activity for a CountDown timer in Android?
I am trying to create a countdown timer that counts down for 60 seconds (optionally skippable by the user). That part of the code works. How do I make it so an action is taken upon the completion of the countdown timer (the same action as the button does, end开发者_JAVA百科ing the activity).
public void startCountdown(int total, final int increase) {
final TimerClassExtended timer = new TimerClassExtended(total,1000);
timer.start();
Button skip = (Button)findViewById(R.id.skip);
skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
timer.cancel();
setResult(RESULT_OK);
finish();
}
});
}
Figured it out, had to modify the following line. But I can't answer myself for 8 hours...
For future reference, TimerClassExtended is just a class I made that extends CountDownTimer so I could add extra methods that I needed.
final TimerClassExtended timer = new TimerClassExtended(total,1000) {
public void onFinish() {
setResult(RESULT_OK);
finish();
}
};
I have no knowledge of the class you are using, but you might consider using a TimerTask
and scheduling it for 60000ms.
Timer timer = new Timer();
timer.schedule( task, 60000 );
Since I had a custom class I added the following:
final TimerClassExtended timer = new TimerClassExtended(total,1000) {
public void onFinish() {
setResult(RESULT_OK);
finish();
}
};
精彩评论