Android handler HandlerThread usage

Source: Internet
Author: User

Android handler HandlerThread usage

I. Handler

Handler is responsible for sending and processing messages in android. Its main purposes include:

1) send a message as planned or execute a Runnanble (using the POST method );
2) put messages sent from other threads into the message queue to avoid thread conflicts (often used in updating UI threads)
By default, Handler accepts the message loop instances in the current thread (using Handler (loophtling), Handler (loophtling, Handler. callback callback) can specify a thread). At the same time, a message queue can be distributed and processed by multiple objects in the current thread (in the UI thread, the system has an Activity to process, you can initiate several Handler operations ). When Handler is instantiated, logoff can be any thread. As long as there is a Handler pointer, any thread can also sendMessage. Handler does not process messages concurrently. A logoff reads the next Message only after processing a Message. Therefore, the processing of the Message is a blocking (handleMessage () method, and there should be no time-consuming operations, the time-consuming operation can be executed in other threads. After the operation is completed, the Message is sent (by using the sendMessges method), and then the handleMessage () is used to update the UI ).

HandlerThread inherits from Thread, so it is essentially a Thread. The difference with a normal Thread is that it has a logoff member variable. This loose is actually the encapsulation of the message queue and the queue processing logic. Simply put, it is the message queue + message loop.

When we need a worker thread, instead of using it as a disposable consumable, we can use it if it is used out.




Package com. android. settings;


Import android. OS. Bundle;
Import android. app. Activity;
Import android. OS. Handler;
Import android. OS. Message;
Import android. text. format. Time;
Import android. util. Log;
Import android. view. Menu;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;

Public class BeidouDetailSettingsActivity extends Activity {

Private MyHandlerThread myHandlerThread = null;
Private Button mTestBtn;
Private TextView mTestTV;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

MTestBtn = (Button) findViewById (R. id. main_test_btn );
MTestTV = (TextView) findViewById (R. id. main_test_ TV );

MTestBtn. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View view ){
Message msg = new Message ();
Msg. what = 1;
MyHandlerThread. sendMessage (msg );
// MHandler. sendMessage (msg );
}
});

MyHandlerThread = new MyHandlerThread ("background thread .");
MyHandlerThread. setmMainHandler (mHandler );
}

Final Handler mHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
If (msg. what = 2 ){
Bundle bundle = msg. getData ();
String threadID = bundle. getString ("id ");
String threadName = bundle. getString ("name ");
Time time = new Time ();
Time. setToNow ();

Log. d ("tianxuhong ", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
String timeStr = time. format3339 (true );

StringBuilder sb = new StringBuilder ("id:"). append (threadID)
. Append ("\ nname:"). append (threadName). append ("\ n ")
. Append ("time:"). append (timeStr );
MTestTV. setText (sb. toString ());
}
}
};

}



Package com. android. settings;

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

/**
* Created by dev on 11/25/13.
*/
Public class MyHandlerThread {
Private HandlerThread mHandlerThread = null;
Private MyHandler mHandler = null;
Private Handler mMainHandler = null;

Public MyHandlerThread (String threadName ){
Super ();

MHandlerThread = new HandlerThread (threadName );
MHandlerThread. start ();

MHandler = new MyHandler (mHandlerThread. getLooper ());
}

Public void setmMainHandler (Handler handler ){
This. mMainHandler = handler;
}

Public void sendMessage (Message msg ){
MHandler. sendMessage (msg );
}

Class MyHandler extends Handler {
MyHandler (){
}

MyHandler (low.lofter ){
Super (logoff );
}

@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Message toMainMsg = mMainHandler. obtainMessage ();

Log. d ("tianxuhong", "toMainMsg =" + toMainMsg );
ToMainMsg. what = 2;
Bundle bundle = new Bundle ();
String threadID = String. valueOf (Thread. currentThread (). getId ());
String threadName = Thread. currentThread (). getName ();

Bundle. putString ("id", threadID );
Bundle. putString ("name", threadName );
ToMainMsg. setData (bundle );

MMainHandler. sendMessage (toMainMsg );
}
}
}

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.