Reputation: 3
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start()
this helps me create a timer but how can i create a timer as soon as the above timer finishes and generate a notification when the timer finishes
Upvotes: 0
Views: 431
Reputation: 369
This should work fine
package com.stackoverflow.example; import android.os.CountDownTimer; /** * @author aminu on 5/7/2016. */ public class CountDownTimerHelper { private int numOfRuns; private long millisInFuture, countDownInterval; private CountDownTimerHelper(int numOfRuns, long millisInFuture, long countDownInterval) { //you may check for invalid arguments here like negative intervals this.numOfRuns = numOfRuns; this.millisInFuture = millisInFuture; this.countDownInterval = countDownInterval; } private void tryRun() { if (numOfRuns-- > 0) { new CountDownTimerImpl(millisInFuture, countDownInterval).start(); } } public static void startCountDownTimers(int numOfRuns, long millisInFuture, long countDownInterval) { new CountDownTimerHelper(numOfRuns, millisInFuture, countDownInterval).tryRun(); } private class CountDownTimerImpl extends CountDownTimer { /** * @param millisInFuture The number of millis in the future from the call * to {@link #start()} until the countdown is done and {@link #onFinish()} * is called. * @param countDownInterval The interval along the way to receive * {@link #onTick(long)} callbacks. */ private CountDownTimerImpl(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long millisUntilFinished) { //mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } @Override public void onFinish() { //notify user countdown ended by showing toast or something like that //mTextField.setText("done " + "remaining " + numOfRuns + "); tryRun(); } } }
then to use it just do CountDownTimerHelper.startCountDownTimers(numOfRuns,yourDuration,yourInterval)
example
CountDownTimerHelper.startCountDownTimers(5,15000,1000) //this will run 5 successive countdowntimers with duration 15 seconds and interval 1 second
Upvotes: 0
Reputation: 2874
First : Make a class that extends CountDownTimer
public class SecondTimer extends CountDownTimer{
public SecondTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
//Generate notification here
}
}
Second : Initialize the instance of that class
SecondTimer secondTimer = new SecondTimer(5000,1000);
Third : Start SecondTimer when First timer stops
public class FirstTimer extends CountDownTimer{
public SecondTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
secondTimer.start();
}
}
FirstTimer firstTimer = new FirstTimer(5000,1000);
firstTimer.start();
Upvotes: 1
Reputation: 648
You shouldn't define the CountDownTimer as an anonymous class, but instead in its own file. Then you could just call: new CountDownTimer(30000, 1000) to start it in the onFinish() method of the previous one. Foer the notification, you could e. g. use Toasts or Snackbars.
Upvotes: 0