开发者

Show elapsed time since pressing a button in Android

开发者 https://www.devze.com 2022-12-24 23:40 出处:网络
I want to 开发者_运维技巧be able to show the elapsed time in a textview or Chronometer held in a \"Statistics\" class since pressing a button located in another class. What would be the easiest way to

I want to 开发者_运维技巧be able to show the elapsed time in a textview or Chronometer held in a "Statistics" class since pressing a button located in another class. What would be the easiest way to implement this?.

Thanks


What about setting a variable = System.currentTimeMillis() when you inflate the view? And at onDestroy substract that time from the current time ?


Set up a tenth-second repeating timer and do the view update in a runnable handler.



    protected Timer timeTicker= new Timer("Ticker");
    private     Handler timerHandler    = new Handler();
    protected   int     timeTickDown        = 10;

    // onCreate() code

    timeTicker.scheduleAtFixedRate(tick, 0, 100); // 100 ms each

    // timer handlers


    protected TimerTask tick = new TimerTask() {
        public void run() {
            myTickTask();
        }
    };

    // Override this in Subclass to get or add specific tick behaviors
    protected void myTickTask() {
        if (timeTickDown == 0) { 
            timerHandler.post(doUpdateTimeout);
        }
        timeTickDown--;

    }

    private Runnable doUpdateTimeout = new Runnable() {
        public void run() {
            updateTimeout();
        }
    };

    private void updateTimeout() {
        timeTickDown = 10; // 10* 100ms == once a second
        // do something useful like sequencing a state machine
        // and gui babble.
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消