Android handler learning notes, androidhandler
- Call Message. obtain () to obtain a message object from the message pool, instead of simply creating a Message object, which can save memory overhead. You can also use handler. obtainMessage (). In fact, it is the same. obtainMessage () is to return Message. obtain ()
- Message. sendToTarget () is the same as handler. sendMessage ().
- The following method can intercept messages.
Private Handler handler = new Handler (new Callback () {public boolean handleMessage (Message msg) {// You can intercept the message in Callback. // if true is returned, the following handleMessage will not be executed. If (msg. what = 1) return true; return false ;}) {public void handleMessage (Message msg) {// process Message }};
- Handler sends messages and adds them to the MessageQueue queue. Logoff receives the messages sent by Handler, polls the MessageQueue queue, and then returns the messages to Handler.
- Message is bound to handler. Available
Message msg = handler. obtainMessage (); msg. sendToTarget (); Message msg = Message. obtain (handler); msg. sendToTarget (); Message msg = Message. obtain (); handler. sendMessage (msg); or manually bind it to use msg. setTarget (handler );
- HandlerThread automatically contains logoff and does not need to be manually created. It is easy to use.
- Because the main thread contains a logoff by default, you do not need to input a logoff. To use Handler in a child thread, You need to input the Logoff of a thread. If the thread is handlerThread, you can directly use handlerThread. getlogoff () to get the logoff. If it is a self-written thread, create a looper in it, because different threads handle it, it is likely to report a null pointer error.
- The method for creating a logoff for a common thread is as follows: Call logoff first in the run () method of the thread. prepare () initializes logoff, and then the run () method finally calls logoff. loop (), so that we can create a logoff in this thread. (Note: The logoff. loop () method is an endless loop by default ). HandlerThread can save trouble.
- Handler associates a separate thread with a message queue.
- Only something in the run is running in the sub-thread.
The following is an example of handler:
Package com. ac. handlertest; 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; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity implements OnClickL Istener {private Layout layout; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initLayout (); initValue ();} private void initLayout () {layout = new Layout (); layout. helloText = (TextView) findViewById (R. id. helloText); layout. button1 = (Button) findViewById (R. id. button1); layout. button2 = (Button) findView ById (R. id. button2); layout. button3 = (Button) findViewById (R. id. button3); layout. button1.setOnClickListener (this); layout. button2.setOnClickListener (this); layout. button3.setOnClickListener (this);} private void initValue () {layout. helloText. setText (""); layout. button1.setText ("1. handler of the main thread "); layout. button2.setText ("2. handler "); layout. button3.setText ("3. use HandlerThread ");} private class Layout {TextView helloText; Button button1; Button button2; Button button3;} // 1. handler private void method_1 () {new Handler (). postDelayed (new Runnable () {@ Override public void run () {Log. I ("msg", "" + Thread. currentThread (); // The main thread layout. helloText. setText ("because the main thread contains a logoff by default, you do not need to input a logoff. ") ;}}, 1000) ;}// 2. handler private void method_2 () {Thread thread = new Thread (new Runnable () {@ Override public void run () {Looper. prepare (); Handler handler = new Handler () {public void handleMessage (Message msg) {Log. I ("msg", "" + Thread. currentThread (); // sub-thread myThread1 runOnUiThread (new Runnable () {public void run () {layout. helloText. setText ("the method for creating logoff for a common thread is as follows: Call Lo first in the thread run () method Callback. prepare () initializes logoff, and then the run () method finally calls logoff. loop (), so that we can create logoff in this thread. (Note: The logoff. loop () method is an endless loop by default ). ") ;}};};}; Try {Thread. sleep (1000); handler. sendEmptyMessage (0);} catch (InterruptedException e) {e. printStackTrace ();} logoff. loop () ;}}); thread. setName ("myThread1"); thread. start ();} // 3. use HandlerThread private void method_3 () {HandlerThread handlerThread = new HandlerThread ("myThread2"); handlerThread. start (); Handler handler = new Handler (handlerThread. getLooper () {public void handleMessage (Message msg) {Log. I ("msg", "" + Thread. currentThread (); // sub-thread myThread2 runOnUiThread (new Runnable () {@ Override public void run () {layout. helloText. setText ("and handlerThread can directly use handlerThread. getLooper () to get the looper, you don't need to worry about looper, you can save trouble ") ;}}}; handler. sendEmptyMessage (0) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. button1: method_1 (); break; case R. id. button2: method_2 (); break; case R. id. button3: method_3 (); break; default: break ;}}}MainActivity. java
Complete demo: http://pan.baidu.com/s/1qWwTs3y