Package com. example. handlertest; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/*** use a demo to explain the Handler processing situations. *** to introduce the Handler message mechanism, we must first know the difference between "synchronous" and "Asynchronous" Communication **. ** synchronous communication: for example, I called James and talked with James., I have to wait until I end the call with James before I can call other people * (synchronous means that after a request is sent, nothing will happen, wait until the result is returned.) ** "Asynchronous" Communication: for example, after I send an e-mail to several students, I don't need to wait for them to reply here. I can do other things. When they reply, the mailbox system will notify me * (after the asynchronous request is sent, after the request is completed, the Status, notification, or callback method is used to notify the caller of the processing result. *** cause of Handler mechanism: after an application is enabled, first, a UI thread (main thread) is enabled. As the name suggests, it is used to manage UI controls on the interface. * It distributes events, such as a Button click event, android distributes events to corresponding buttons to respond to user operations * However, users may need to perform time-consuming operations (download files ), however, if the android interface does not respond in five seconds, the system will prompt * the user closes the application. These time-consuming operations need to be processed in the sub-thread. After the sub-thread completes processing, the UI in * UI thread is updated. The Android UI thread is insecure, this means that the UI thread interface cannot be directly updated in the Child thread. * therefore, Handler is designed in Android to solve this problem! ** Solution: Handler runs in the UI thread. It and the sub-thread can transmit data through the Message object. At this time, handler is responsible for receiving the Message object sent from the sub-thread *, so as to update the UI with the UI thread. * ** Handler message mechanism: *** 1. handler: ** Handler allows you to send and process Message messages. Message messages are accessed through the Message and * Runnable objects associated with the MessageQueue Message queue of the main thread, when a new handler is created (created in the main thread), the handler belongs to the current main thread, and the * MessageQueue of the main thread is also created simultaneously, handler is bound to the main thread/Message queue. In this way, handler can send and receive Message objects through the Message Queue * of the main thread. handler Message Processing Mechanism in several situations [the following code is used to describe these situations]: 2.1 button1: Handler 2.2 button2 in the main thread: * Handler 2.3 button3 in the Child thread: pass the Handler created by the main thread to other threads to complete the message 2.4 button4: * update the UI thread interface in other threads. It throws an exception. Let's take a look ~! * *** Sending a Message does not block the thread (asynchronous ), the processing of received messages will block the thread [When Handler processes a Message object, it will continue to fetch the next Message for processing] (synchronous) * ** September 11, 2013 23:37:08 ** @ author xiaoyaomeng **/public class MainActivity extends Activity implements OnClickListener {HandlerDemo myHandlerDemo = null; // This object is used to create handler Button button1 = null in the main thread; button button2 = null; Button button3 = null; Button button4 = null; @ Override protected void onCreate (Bundle SavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. button1); button2 = (Button) findViewById (R. id. button2); button3 = (Button) findViewById (R. id. button3); button4 = (Button) findViewById (R. id. button4); button1.setOnClickListener (this); button2.setOnClickListener (this); button3.setOnClickListener (this); button4.setO NClickListener (this) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub handlerDealMessage (v. getId ();} private void handlerDealMessage (int id) {// TODO Auto-generated method stub Switch (id) {case R. id. button1: {// create the Handler instance myHandlerDemo = new HandlerDemo () in the main thread; Message message = myHandlerDemo. obtainMessage (1, (Object) ("Hello, My name is Handler1 ~~ "); Message. sendToTarget (); // the Handler sends a message to the UI thread bound to the Handler for processing} break; case R. id. button2: {MyThread myThread = new MyThread (); myThread. start ();} break; case R. id. button3: {myHandlerDemo = new HandlerDemo (); OtherThread otherThread = new OtherThread (myHandlerDemo); otherThread. start ();} break; case R. id. button4: {errorHandlerThread = new errorHandlerThread (button4 ); ErrorHandlerThread. start ();} break; default: break;}/** MyThread is an internal class, sub-Thread */private class MyThread extends Thread {@ Override public void run () {// TODO Auto-generated method stub super. run (); // 1. error: the Handler object can be created only in the UI thread without the logoff object. Therefore, an exception is reported here. // myHandlerDemo = new HandlerDemo (); // 2. error: logoff. the logoff value obtained by mylogoff is null, so an exception is reported. // myHandlerDemo = new HandlerDemo (logoff. mylogoff ()); // Use logoff. getMainLooper () can get the looper of the parent class, so you can successfully create a handler object and bind MessageQueue myHandlerDemo = new HandlerDemo (lodemo. getMainLooper (); Message message = myHandlerDemo. obtainMessage (2, (Object) ("Hello, My name is Handler2 ~~ "); Message. sendToTarget () ;}} private/*** create a Handler ** @ author xiaoyaomeng **/class HandlerDemo extends Handler {/* When you create a handler in the UI thread, you can directly call this constructor */public HandlerDemo () {super (); // TODO Auto-generated constructor stub}/* This constructor must be used to create a Handler in the sub-thread. Otherwise, an error is returned */public HandlerDemo (low.low.low.) {super (low ); // TODO Auto-generated constructor stub} @ Override public void handleMessage (M Essage msg) {// TODO Auto-generated method stub super. handleMessage (msg); switch (msg. what) {case 1: {button1.setText (msg. obj. toString ();} break; case 2: {button2.setText (msg. obj. toString ();} break; case 3: {button3.setText (msg. obj. toString ();} break; default: break; }}}/*** other thread classes, used to receive a handler, and use this handler to send messages in the Thread ** @ author xiaoyaomeng **/class OtherThread extends Thread {Private Handler mHandler; public OtherThread (Handler handler) {// TODO Auto-generated constructor stub mHandler = handler;} @ Override public void run () {// TODO Auto-generated method stub super. run (); Message message = mHandler. obtainMessage (3, (Object) ("Hello, My name is Handler3 ~~ "); Message. sendToTarget () ;}}/*** Thread for Testing Error Reporting ** @ author xiaoyaomeng **/class errorHandlerThread extends Thread {Button button = null; public errorHandlerThread (Button button) {// TODO Auto-generated constructor stub this. button = button;} @ Override public void run () {// TODO Auto-generated method stub super. run (); button. setText ("hahahaha ~~~ ");}}