Handler mainly accepts the data sent by the sub-thread and uses the data to update the UI with the main thread.
Handler can distribute the Message object and Runnable object to the main thread. Each Handler instance is bound to the thread that creates the Handler (usually located in the main thread). It has two functions: (1): Schedule a message or Runnable to be executed somewhere in a main thread. (2) Schedule an action to be executed in different threads.
In the previous article, we talked about the implementation of multithreading. Here we will talk about how to implement the runnable interface based on an instance to implement multithreading and Handler usage.
Ask a question: how can I have the program update the Title in five seconds?
First, let's take a look at the Java programmers who are used to writing programs before they know how to use Handler. The Code is as follows:
Public class TestActivity extends Activity {
Private int count = 1;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
New Thread (new TitleThread (). start ();
}
Class TitleThread implements Runnable {
@ Override
Public void run (){
While (true ){
SetTitle ("Welcome to my blog" + count ++ );
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}
However, when we execute a program, we cannot achieve the expected results. Therefore, Android introduces the special class Handler, which can be said to be a bridge between Runnable and Activity, therefore, we only need to send a Message in the run method, and execute different tasks through different messages in Handler.
The modified code is as follows:
Public class MainActivity extends Activity {
Private int count = 1;
Private Handler handler = new Handler (){
Public void handleMessage (android. OS. Message msg ){
Switch (msg. what ){
Case 1:
UpdateTitle ();
Break;
Default:
Break;
}
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
New Thread (new TitleThread (). start ();
}
Protected void updateTitle (){
SetTitle ("Welcome to my blog" + count ++ );
}
Class TitleThread implements Runnable {
@ Override
Public void run (){
While (true ){
Message message = new Message ();
Message. what = 1;
Handler. sendMessage (message );
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}
In this way, the title will be updated within 5s after running ....
In addition, the Timer class and TimerTask can be used for thread creation. The Code is as follows:
Public class TimerActivity extends Activity {
Private int count = 1;
Private Handler handler = new Handler (){
Public void handleMessage (android. OS. Message msg ){
Switch (msg. what ){
Case 1:
UpdateTitle ();
Break;
Default:
Break;
}
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Timer timer = new Timer ();
Timer. scheduleAtFixedRate (new TimerTask (){
@ Override
Public void run (){
Message message = new Message ();
Message. what = 1;
Handler. sendMessage (message );
}
}, 1000,5000 );
}
Protected void updateTitle (){
SetTitle ("Welcome to my blog" + count ++ );
}
}