我是Android和Java的新手,我一直在尝试解决计时器问题。请帮助 我有一个应用程序,用户可以创建多个倒计时器和多个秒表。 倒数计时器的示例代码如下所示(秒表也以类似的方式使用处理程序实现)。即使用户离开应用程序,我也希望定时器和秒表能够在后台运行。当用户再次返回应用程序时,应显示当前的计时器状态,而不是显示初始状态。 问题是我离开应用程序时我的计时器和秒表正在运行,但当用户返回应用程序时,结果不会发布到用户界面中。创建一个新实例,再次显示00:00:00的新计时器,而旧计时器继续在后台运行并继续运行而不能停止。在下面的代码中我有一个监听器发布结果在其回调中进入UI。这个界面在我的活动中实现。那么有人可以为此建议一个解决方案吗?
public class CountDownTimerService {
public interface CounterTimeChangeListener {
public void onCounterTimeChange(String time,Boolean counterFinished);
}
CounterTimeChangeListener listener;
MyCountDownTimer myCountDownTimer;
String output;
int length;
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
public CountDownTimerService(int countDownFutureTime)
{
myCountDownTimer= new MyCountDownTimer(countDownFutureTime,1000);
length=countDownFutureTime;
}
// code for countdown timer
// this function format the time to hours seconds and minutes format
public String formatTime(long mills)
{
output = "";
long seconds = mills / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD +":" +minutesD +":"+ secondsD;
return output;
}
// this function is called on start button click
public void CountDownControl(int state) {
if(state==0)
{
startTime = System.currentTimeMillis();
myCountDownTimer.start();
}
else if(state==1)
{
// when the pause is clicked
myCountDownTimer.cancel();
currentTime = System.currentTimeMillis();
timeElapsed = currentTime - startTime;
if (prevTimeRemaining == 0)
timeRemaining = length - timeElapsed;
else
timeRemaining = prevTimeRemaining - timeElapsed;
prevTimeRemaining = timeRemaining;
myCountDownTimer= new MyCountDownTimer(timeRemaining,1000);
if (listener!=null) {
listener.onCounterTimeChange(formatTime(timeRemaining), false);
}
}
}
// this function is called on reset button click
public void resetInitial()
{
prevTimeRemaining = 0;
myCountDownTimer.cancel();
if (listener!=null) {
listener.onCounterTimeChange(formatTime(length),false);
}
myCountDownTimer= new MyCountDownTimer(length,1000);
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onTick (long millisUntilFinished) {
// calls the fragment's callback function passing the time value
Log.e("in counter","1");
if (listener!=null) {
Log.e("in counter","2");
listener.onCounterTimeChange(formatTime(millisUntilFinished), false);
}
}
public void onFinish() {
/*
* write code for an alarm when timer times out
*
* */
prevTimeRemaining = 0;
if (listener!=null) {
listener.onCounterTimeChange(formatTime(length),true);
}
myCountDownTimer= new MyCountDownTimer(length,1000);
}
}
public void setCounterTimeChangeListener(CounterTimeChangeListener listener) {
this.listener = listener;
}
}