Android控制器如何实现包含Runnable Observable的Observer

时间:2012-04-26 02:50:50

标签: android runnable observable observers

我正在写一个Cowndown计时器,我有一个countdowntimerController,它实现了Observer接口。它有一个名为countdown的内部类,它扩展了Observable类并实现了Runnable接口。但是,当runnable完成时,我的控制器不会收到通知。以下是我的代码,请告诉我哪里出错了。附:既然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);
        }
    };
}

0 个答案:

没有答案