我正在编写一个 Cowndown 计时器,并且我有一个 countdowntimerController,它实现了 Observer 接口。它有一个名为 countdown 的内部类,它扩展了 Observable 类并实现了 Runnable 接口。但是,当可运行对象完成时,我的控制器不会收到通知。以下是我的代码,请告诉我哪里出错了。PS 因为 Observer 在 Observable 内部,所以我使用了 countdown.addObserver(this),这会是问题吗?
public class CountdownTimerController extends ActivityController implements Observer {
private Handler handler = new Handler();
private void startTimer(int posi) {
/* some countdown timer codes */
Countdown countdown = new Countdown();
countdown.addObserver(this);
handler.post(countdown);
}
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
Log.d("countdownController", "update");
}
private class Countdown extends Observable implements Runnable {
//private Handler handler = new Handler();
public void run() {
long millis = countdownTime - (System.currentTimeMillis() - startTime) + pausedTime;
if(millis <=0) {
onFinish();
return;
}
/* time calculation logic */
handler.postDelayed(this, 100);
}
public void onFinish(){
notifyObservers();
handler.removeCallbacks(this);
}
};
}