Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler postDelayed - print every second something

I want to print the current second using a handler. I record a video for exactly 10 seconds and want to set the text of a TextView every second.

Recording 10 seconds works like that:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
           stopRecordingVideo();
    }
}, 11000);  // don't know why 11000 but it only works this way

After the 10 seconds the method stopRecordingVideo() gets executed. So how can I change the text every second of the TextView?

like image 406
mafioso Avatar asked Jan 06 '17 18:01

mafioso


People also ask

What is new handler () postDelayed?

postDelayed(Runnable r, Object token, long delayMillis) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. final void.

What is the difference between Android timer and a handler to do action every n seconds?

Handler is better than TimerTask . The Java TimerTask and the Android Handler both allow you to schedule delayed and repeated tasks on background threads. However, the literature overwhelmingly recommends using Handler over TimerTask in Android (see here, here, here, here, here, and here).

Does handler run on UI thread?

Android handles all the UI operations and input events from one single thread which is known as called the Main or UI thread. Android collects all events in this thread in a queue and processes this queue with an instance of the Looper class.

What is handler and looper in Android?

Looper is an abstraction over event loop (infinite loop which drains queue with events) and Handler is an abstraction to put/remove events into/from queue with events (which is drained by Looper) and handle these events when they are processed.


2 Answers

Working answer:

int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        t++;
        textView.setText(getString(R.string.formatted_time, t));
        if(t<10) {
            handler.postDelayed(this, 1000);
        }
    }
}, 1000);

Where formatted_time is something like that:

<string android:name="formatted_time">%d seconds</string>

like image 121
mafioso Avatar answered Nov 13 '22 00:11

mafioso


To print text every second, you can use CountDownTimer. But if you want to achieve this with try below code:

void startTime(){
    //Post handler after 1 second.
    handler.postDelayed(runnable, 1000);
}

int totalDelay=0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        totalDelay++;   
        if(totalDelay<=10){
            //If total time is less then 10 second execute handler again after 1 second
            handler.postDelayed(runnable, 1000);
        }
        textView.setText(totalDelay+" Second");
    }
};
like image 36
Dhaval Patel Avatar answered Nov 13 '22 00:11

Dhaval Patel