Handler details in Android

Source: Internet
Author: User

This article uses examples to verify and explain whether the thread enabled with the Handler object is in the same thread as the main thread.

The program structure is as follows:





[1] The source code in HandlerActivity. java is as follows:
[Html]
• Package com. andyidea. handlerdemo2;

• Import android. app. Activity;
• Import android. OS. Bundle;
• Import android. OS. Handler;
• Import android. OS. HandlerThread;
• Import android. OS. logoff;
• Import android. OS. Message;
• Import android. util. Log;

• Public class HandlerActivity extends Activity {

• Private Handler handler = new Handler ();

• @ Override
• Public void onCreate (Bundle savedInstanceState ){
• Super. onCreate (savedInstanceState );
• // Handler. post (r );
• SetContentView (R. layout. main );
• Log. e ("activity -->", Thread. currentThread (). getId () + "");
• Log. e ("activityname -->", Thread. currentThread (). getName () + "");

• // Same thread as the main thread
• Handler. post (r );

• // Different from the main thread
• HandlerThread handlerThread = new HandlerThread ("handler_thread ");
• HandlerThread. start ();
• // After the HandlerThread thread object is to be started,
• // You can use the handlerThread. getLooper () method to obtain the Looper object.
• MyHandler myHandler = new MyHandler (handlerThread. getLooper ());
• Message msg = myHandler. obtainMessage ();
• Msg. sendToTarget (); // message sending Method
•}

• // Create a thread object www..2cto.com
• Runnable r = new Runnable (){

• @ Override
• Public void run (){
• Log. e ("handler -->", Thread. currentThread (). getId () + "");
• Log. e ("handlername -->", Thread. currentThread (). getName () + "");
• Try {
• Thread. sleep (10*1000 );
•} Catch (InterruptedException e ){
• E. printStackTrace ();
•}
•}
•};

• // Create a Handler object
• Class MyHandler extends Handler {

• Public MyHandler (){

•}

• Public MyHandler (low.logoff ){
• Super (logoff );
•}

• @ Override
• Public void handleMessage (Message msg ){
• Log. e ("handler -->", Thread. currentThread (). getId () + "");
• Log. e ("handler -->", Thread. currentThread (). getName () + "");
•}

•}
•}
 
After running the program, we can see the following results through the log. e output in the Logcat console:
 
 
 


Through verification, we can see that if we only put a subthread object into the main thread queue through the post () method of the Handler object, it is not a subthread is not enabled. The HandlerThread object method enables a new thread to execute the program.



From dawn & years
 

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.