Introduction to Handler in Android multi-thread processing

Source: Internet
Author: User

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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.