问题
I have an app with multiple timers; when I start one, I can see it counting down in Logcat in Eclipse.
When I hit the Back button, onStop()
is called in the app but the timer continues to countdown in logcat (the onTick()
is continuing to tick away).
What I would like is when onResume()
is called, I want to get that timer which is still counting down and continue it. Is this possible?
Here are my buttons that start the countdown:
//Button1 Timer
final CountDown button1 = new CountDown(420000,1000,bButton1);
bButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button1.start();
long currentTime = System.currentTimeMillis();
//Set start time for calculating time elapsed if needed
saveTime("START_TIME_BUTTON1", currentTime);
}
});
//Button2 Timer
final CountDown button2 = new CountDown(360000,1000,bButton2);
bButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button2.start();
long currentTime = System.currentTimeMillis();
//Set start time for calculating time elapsed if needed
saveTime("START_TIME", currentTime);
}
});
My onResume()
looks like this:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Reset the timers on appropriate buttons if there was something running
//If nothing was running or timer finished - have button in default state
//See if there was a button1 timer running - if not, it will be 0
SharedPreferences start = PreferenceManager.getDefaultSharedPreferences(this);
long startTime = start.getLong("START_TIME_BUTTON1", 0);
long timeElapsed = (System.currentTimeMillis()-startTime);
long timeRemaining = (420000 - timeElapsed);
if (timeRemaining == 0) {
} else if (timeRemaining > 0) {
final CountDown button1Timer = new CountDown(timeRemaining,1000,bButton1);
button1Timer.start();
long currentTime = System.currentTimeMillis();
saveTime("START_TIME", currentTime);
} else {
}
}
This actually works — the onResume()
starts another timer right where the first one would be, and the text displays the appropriate number and continues counting down with the onTick()
method. But now logcat shows 2 timers counting down instead of just one!
Ultimately I wouldn't want to start another timer, I just want to pick up the first timer I started where it is currently in the countdown at and have the onTick()
display appropriately. Is there a way to do that? Would services be what I'm looking for? Is it already a service since it continues to tick down in the background?
I'm a little confused on what would be best practice to get this done.
回答1:
onTick()
will continue being called until either the time remaining reaches 0 or you call cancel()
on the CountDownTimer.
So in your onStop()
method you will need to call cancel()
on the timer you want to stop receiving onTick()
notifications.
Android's implementation of CountDownTimer uses a Handler
to perform timing by queuing a Message
to be sent to the Handler
after each tick. Unfortunately there is no way to pause the CountDownTimer, so I believe you will need to create a new Timer
with the appropriate values like you're doing currently.
When you're calculating the timeElapsed, you shouldn't use System.currentTimeMillis()
because it can be changed at any time, instead you should use SystemClock.elapsedRealtime()
. System.currentTimeMillis()
is the timer used for the wall clock (time and date) and so can change unpredictably, see the Android documentation.
来源:https://stackoverflow.com/questions/15144232/countdowntimer-continues-to-tick-in-background-how-do-i-retrieve-that-count-in