First, Handler
Handler is responsible for sending and processing messages on Android. Its main uses are:
1) Send messages on a schedule or execute a runnanble (using the Post method);
2) messages sent from other threads into the message queue to avoid thread conflicts (common in updating the UI thread)
By default, Handler accepts a message loop instance under the current thread (using Handler (Looper Looper), Handler (Looper Looper, Handler.callback Callback) to 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 already has an activity to handle, and you can take a number of handler to handle it). When instantiating handler, Looper can be any thread, as long as there are handler pointers, any thread can sendMessage. Handler handling of message is not concurrent. A looper will read the next one only after processing one message, so the processing of the message is blocked (the Handlemessage () method should not have a time-consuming operation that can place time-consuming operations on other threads. Send a message after the operation (via the Sendmessges method), and then update the UI by Handlemessage ().
Handlerthread inherits from thread, so it's essentially a thread. The difference from normal thread is that it has a looper member variable. This looper is actually the encapsulation of the message queue and the queue processing logic, which is simply the message queue + message loop.
When we need a worker thread instead of treating it as a disposable consumable, it can be used if it is discarded.
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.Looper;
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 (Looper Looper) {
Super (Looper);
}
@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);
}
}
}
Android Handler Handlerthread usage