- Calling Message.obtain () to get a message object from the pool of messages, rather than just a new message object, can save memory overhead. can also use Handler.obtainmessage (), is actually the same, Obtainmessage () is to return Message.obtain ()
- Message.sendtotarget () is the same as Handler.sendmessage ()
- The following method can intercept a message.
PrivateHandler handler=NewHandler (NewCallback () { Public Booleanhandlemessage (Message msg) {//the message can be intercepted in the callback//This returns true and no longer executes the following handlemessage. if(msg.what==1)return true; return false; } }){ Public voidhandlemessage (Message msg) {//processing Messages } };
- Handler sends a message and joins the MessageQueue queue. Looper receives the message sent by handler, polls the MessageQueue queue, and then passes it back to handler.
- Message binding handler. can be used
Message msg=handler.obtainmessage (); Msg.sendtotarget (); Message msg=Message.obtain (handler); Msg.sendtotarget (); Message msg=message.obtain (); Handler.sendmessage (msg), or manual binding, using msg.settarget (handler);
- Handlerthread automatically contains looper, it does not need to be created manually, easy to use.
- Because a looper is included by default in the main thread, there is no need to pass in Looper. Using handler in a child thread requires passing in a thread's looper, and if thread is Handlerthread, you can get Looper directly with Handlerthread.getlooper (). If you write a thread, create a looper in it, because different threading, it is possible to report a null pointer error.
- the method for creating looper for normal threads is as follows: Call Looper.prepare () to initialize Looper in the thread run () method before the run () method finally calls Looper.loop (). This allows us to create good looper in this thread. (Note: the Looper.loop () method is a dead loop by default). Using Handlerthread can be a hassle.
- The handler is associated with a separate thread and message queue.
- Only the thing in run is running in a child thread.
Here's an example, handler example:
Packagecom.ac.handlertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.HandlerThread;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateLayout layout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initlayout (); InitValue (); } Private voidInitlayout () {layout=NewLayout (); Layout.hellotext=(TextView) Findviewbyid (R.id.hellotext); Layout.button1=(Button) Findviewbyid (R.id.button1); Layout.button2=(Button) Findviewbyid (R.id.button2); Layout.button3=(Button) Findviewbyid (R.id.button3); Layout.button1.setOnClickListener ( This); Layout.button2.setOnClickListener ( This); Layout.button3.setOnClickListener ( This); } Private voidInitValue () {Layout.helloText.setText (""); Layout.button1.setText ("1. Handler of the main thread"); Layout.button2.setText ("2. Handler of normal sub-threads"); Layout.button3.setText ("3. Use Handlerthread"); } Private classLayout {TextView hellotext; Button button1; Button button2; Button Button3; } //1. Handler of the main thread Private voidmethod_1 () {NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {LOG.I ("MSG", "" + Thread.CurrentThread ());//Main ThreadLayout.helloText.setText ("Because a looper is included by default in the main thread, there is no need to pass in Looper. "); } }, 1000); } //2. Handler of normal sub-threads Private voidmethod_2 () {thread thread=NewThread (NewRunnable () {@Override Public voidrun () {looper.prepare (); Handler Handler=NewHandler () { Public voidhandlemessage (Message msg) {LOG.I ("MSG", "" + Thread.CurrentThread ());//Child thread MyThread1Runonuithread (NewRunnable () { Public voidrun () {layout.hellotext. SetText (The method for creating looper for a normal thread is as follows: Call Looper.prepare () to initialize the Looper in the thread run () method, and then finally call Looper.loop () in the Run () method, so that we can create the looper in the threads. (Note: the Looper.loop () method is a dead loop by default). "); } }); }; }; Try{Thread.Sleep (1000); Handler.sendemptymessage (0); } Catch(interruptedexception e) {e.printstacktrace (); } looper.loop (); } }); Thread.setname ("MyThread1"); Thread.Start (); } //3. Using Handlerthread Private voidmethod_3 () {handlerthread Handlerthread=NewHandlerthread ("MyThread2"); Handlerthread.start (); Handler Handler=NewHandler (Handlerthread.getlooper ()) { Public voidhandlemessage (Message msg) {LOG.I ("MSG", "" + Thread.CurrentThread ());//Child thread MyThread2Runonuithread (NewRunnable () {@Override Public voidrun () {layout.hellotext. SetText ("and Handlerthread can be directly with Handlerthread.getlooper () to get looper, can not looper, can save money"); } }); } }; Handler.sendemptymessage (0); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.button1:method_1 (); Break; Caser.id.button2:method_2 (); Break; Caser.id.button3:method_3 (); Break; default: Break; } }}Mainactivity.java
Full demo:http://pan.baidu.com/s/1qwwts3y
Android Handler Learning Notes