How an Android controller implements an Observer containing a Runnable Observable

I am writing a Cowndown timer, and I have a countdowntimerController that implements the Observer interface. It has an inner class called a countdown, which extends the Observable class and implements the Runnable interface. However, my controller does not receive notifications when runnable ends. Below are my codes, please tell me where I was wrong. Postscript since Observer is inside Observable, I used countdown.addObserver (this), is this a problem?

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);
        }
    };
}
+3
source share

All Articles