您可能应该将您的计数器放在一个服务中,该服务将作为远程进程运行,并在您需要显示计时器的任何地方绑定到该服务,然后使用观察者模式从您的服务 CountDownTimer 的 onTick 方法中调用侦听器回调,只要您想更新观察者:
mCountDownTimer = new CountDownTimer(startTime, 1000) {
//update after each tick
public void onTick(long millisUntilFinished) {
long seconds = (millisUntilFinished / 1000) % 60;
long minutes = ((millisUntilFinished / 1000) / 60) % 60;
long hours = (((millisUntilFinished / 1000) / 60) / 60) % 24;
long days = (((millisUntilFinished / 1000) / 60) / 60) / 24;
if(listeners!=null){
Log.d(TAG, "days:"+days + "hours:"+ hours + "minutes:"+ minutes + "seconds:"+ seconds);
listeners.onTimeChanged(days, hours, minutes, seconds);
}
}
public void onFinish() {
if(listeners!=null){
Log.d(TAG, "timer finished");
listeners.onTimerFinish();
}
}
}.start();