Show elapsed time since pressing a button on Android

I want to show the elapsed time in a text form or a chronometer stored in the "Statistics" class, after clicking a button located in another class. What would be the easiest way to implement this?

thank

+3
source share
2 answers

How about setting the variable = System.currentTimeMillis()when inflating the view? And with onDestroy, subtract this time from the current time?

+5
source

Configure the tenth timer repeater and update the view in the 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.
    }
+4
source

Source: https://habr.com/ru/post/1739216/


All Articles