Android 让计时器在后台滴答作响
如何在转到另一个活动后保持计时器恢复的值,我的问题是,如果我切换到另一个我想使用Android 让计时器在后台滴答作响,android,timer,background,count,countdowntimer,Android,Timer,Background,Count,Countdowntimer,如何在转到另一个活动后保持计时器恢复的值,我的问题是,如果我切换到另一个我想使用SharedReference的活动,它将设置为默认值,但这不会有帮助,因为我需要它在后台不断递减 public void reset() { countDownTimer.cancel(); } private void setTimer() { int time = 5; if(countDownTimer==null)
SharedReference
的活动,它将设置为默认值,但这不会有帮助,因为我需要它在后台不断递减
public void reset()
{
countDownTimer.cancel();
}
private void setTimer() {
int time = 5;
if(countDownTimer==null)
{
totalTimeCountInMilliseconds = 60 * time * 1000;
}
else
{
reset();
totalTimeCountInMilliseconds = 60 * time * 1000;
}
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
//@Override
public void onTick(long leftTimeInMilliseconds) {
seconds = leftTimeInMilliseconds / 1000;
textViewShowTime.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
}
}.start();
}
您应该在
onPause
中停止CountDownTimer
并在onResume
中重新启动,您的文本视图showtime
在活动处于后台时可能无效
如果您需要在每500毫秒内调用某个代码,不管您处于何种活动状态,则考虑使用<代码> AlARMARCHEM/<代码> ./P>在后台运行代码,使用<代码>服务<代码> >