Next, let me introduce how to use Android Handler. Handler can send Messsage and Runnable objects to the Message Queue of the associated thread. Each Handler object is associated with the thread that created it, and each Handler object can only be associated with one thread.
Handler generally has two purposes: 1) to execute a scheduled task, you can schedule it to execute some tasks and simulate the timer. 2) Inter-thread communication. When an Android Application is started, a main thread is created, and the main thread creates a message queue to process various messages. When creating a sub-thread, you can get the Handler object created in the parent thread in your sub-thread to send messages to the Message Queue of the parent thread.
MainActivity. java
[Java]Copy codeThe Code is as follows: package com. example. thread;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. logoff;
Import android. OS. Message;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView TV;
// Message processor
Private Handler handler;
Private static final int TURNTEXT = 1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. activity_main );
System. out. println ("main Thread ----" + Thread. currentThread (). getName ());
TV = new TextView (this );
TV. setText ("hello ");
SetContentView (TV );
// Processor
Handler = new Handler (){
// HandlerMessage method to process the messages sent by handler
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
// The msg. what operation gets the message to be sent. Different processing methods are implemented based on the message id.
Int what = msg. what;
If (what = TURNTEXT ){
TV. setText (System. currentTimeMillis () + "");
}
}
};
Try {
// The main thread is blocked for more than 6 s
Thread. sleep (10000 );
} Catch (InterruptedException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
// After the run method is executed, the thread will die.
New Thread (new Runnable (){
@ Override
Public void run (){
While (true ){
Try {
System. out. println ("sub-thread ----"
+ Thread. currentThread (). getName ());
Thread. sleep (1000 );
// ID of the message to be sent
Handler. sendEmptyMessage (TURNTEXT );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}). Start ();
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}
Package com. example. thread;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. logoff;
Import android. OS. Message;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView TV;
// Message processor
Private Handler handler;
Private static final int TURNTEXT = 1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. activity_main );
System. out. println ("main Thread ----" + Thread. currentThread (). getName ());
TV = new TextView (this );
TV. setText ("hello ");
SetContentView (TV );
// Processor
Handler = new Handler (){
// HandlerMessage method to process the messages sent by handler
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
// The msg. what operation gets the message to be sent. Different processing methods are implemented based on the message id.
Int what = msg. what;
If (what = TURNTEXT ){
TV. setText (System. currentTimeMillis () + "");
}
}
};
Try {
// The main thread is blocked for more than 6 s
Thread. sleep (10000 );
} Catch (InterruptedException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
// After the run method is executed, the thread will die.
New Thread (new Runnable (){
@ Override
Public void run (){
While (true ){
Try {
System. out. println ("sub-thread ----"
+ Thread. currentThread (). getName ());
Thread. sleep (1000 );
// ID of the message to be sent
Handler. sendEmptyMessage (TURNTEXT );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}). Start ();
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}