How does CountDownTimer access the user interface inside the OnTick method?

How does CountDownTimer access the user interface inside the onTick method?

 (new CountDownTimer(10000,1000){ @Override public void onFinish() { // TODO Auto-generated method stub } @Override public void onTick(long millisUntilFinished) { TextView tv = (TextView)findViewById(R.id.tvLCD); tv.setText(Long.toString(millisUntilFinished)); } }).start(); 
+4
source share
3 answers

You can access the user interface from the Activity.runOnUiTread() , View.post() , View.postDelayed() or through Handler . CountDownTimer uses a Handler for this purpose ( source ).

Read this article to understand how to use all of these methods.

+2
source

From the links ( GreCode - Handler ) in @Sergey Glotov's answer, it is clear that the countdown timer does not use a separate thread at all. It is for this reason that you can access all the elements of the user interface. I do not know why they used the handler. But this does not spawn a new stream. It runs in the user interface line itself.

+8
source

CountDownTimer does NOT have any mechanism for accessing the user interface inside the onTick method. More importantly, from the source code you can see that internally it uses a handler that executes when the object is created. Thus, it works in the thread where the timer was created.

The question is incorrect, in your case I can access these views, because maybe you are creating a CountDownTimer as an anonymous class for activity. And if you're lucky, this was done in the user interface thread.

0
source

All Articles