Handler summary - reprinted from http://mobile.51cto.com/aprogram-442833.htm (invasion and deletion)

The classic summary of Handler in Android development

When the application starts, Android first starts a main thread (that is, the UI thread), and the main thread is the UI control in the management interface to distribute events.

Author: Anonymous Source: pin5i | 2014-06-18 14:41

Technology Salon | On April 21, a number of blockchain experts interpreted the application scenarios of blockchain technology!


First, the definition of Handler:

It mainly accepts the data sent by the child thread, and uses this data to cooperate with the main thread to update the UI.

Explanation: When the application starts, Android will first open a main thread (that is, the UI thread), and the main thread is the UI control in the management interface to distribute events. For example, if you click a Button, Android will distribute events to Button to respond to your actions. If you need a time-consuming operation at this time, such as: reading data online, or reading a large local file, you can't put these operations on the main thread. If you put it in the main thread, the interface There will be a suspended animation. If it is not completed within 5 seconds, you will receive an error message "Forced Close" from the Android system. At this time, we need to put these time-consuming operations in a sub-thread, because the sub-thread involves UI updates, and the Android main thread is thread-unsafe, that is, updating the UI can only be updated in the main thread, Operations in child threads are dangerous. At this time, Handler appeared. , to solve this complex problem, since the Handler runs in the main thread (UI thread), it and the child thread can pass data through the Message object. At this time, the Handler is responsible for accepting the transmission from the child thread (the child thread uses The sedMessage() method passes on the Message object, (which contains data), puts these messages into the main thread queue, and cooperates with the main thread to update the UI.

2. Some features of Handler

Handlers can distribute Message objects and Runnable objects to the main thread. Each Handler instance will be bound to the thread that created it (usually in the main thread). It has two functions:

(1) Arrange the message or Runnable to execute somewhere in a main thread;

(2) Arrange an action to be executed in a different thread.

Some methods of distributing messages in Handler

post(Runnable)

postAtTime(Runnable,long)

postDelayed(Runnable long)

sendEmptyMessage (int)

sendMessage(Message)

sendMessageAtTime(Message,long)

sendMessageDelayed(Message,long)

The above post class method allows you to queue a Runnable object to the main thread queue,

The sendMessage class method allows you to arrange a Message object with data into the queue, waiting to be updated.

3. Handler instance

Subclasses need to inherit the Hendler class and override the handleMessage(Message msg) method to receive thread data.

The following is an example, the function it implements is: modify the content of the interface Button through the thread

 
  
  1. publicclass MyHandlerActivity extends Activity {  
  2.     Button button; 
  3.     MyHandler myHandler; 
  4.  
  5.     protectedvoid onCreate(Bundle savedInstanceState) {  
  6.         super 。onCreate (savedInstanceState); 
  7.         setContentView(R。layout。handlertest); 
  8.  
  9.         button = (Button) findViewById(R。id。button); 
  10.         myHandler = new MyHandler(); 
  11.         // 当创建一个新的Handler实例时, 它会绑定到当前线程和消息的队列中,开始分发数据 
  12.         // Handler有两个作用, (1) : 定时执行Message和Runnalbe 对象 
  13.         // (2): 让一个动作,在不同的线程中执行。 
  14.  
  15.         // 它安排消息,用以下方法 
  16.         // post(Runnable) 
  17.         // postAtTime(Runnable,long) 
  18.         // postDelayed(Runnable,long) 
  19.         // sendEmptyMessage(int) 
  20.         // sendMessage(Message); 
  21.         // sendMessageAtTime(Message,long) 
  22.         // sendMessageDelayed(Message,long) 
  23.       
  24.         // 以上方法以 post开头的允许你处理Runnable对象 
  25.         //sendMessage()允许你处理Message对象(Message里可以包含数据,) 
  26.  
  27.         MyThread m = new MyThread(); 
  28.         new Thread(m)。start(); 
  29.     } 
  30.  
  31.     /** 
  32.     * 接受消息,处理消息 ,此Handler会与当前主线程一块运行 
  33.     * */ 
  34.  
  35.     class MyHandler extends Handler { 
  36.         public MyHandler() { 
  37.         } 
  38.  
  39.         public MyHandler(Looper L) { 
  40.             super(L); 
  41.         } 
  42.  
  43.         // 子类必须重写此方法,接受数据 
  44.         @Override 
  45.         public void handleMessage(Message msg) { 
  46.             // TODO Auto-generated method stub 
  47.             Log。d("MyHandler", "handleMessage。。。。。。"); 
  48.             super。handleMessage(msg); 
  49.             // 此处可以更新UI 
  50.             Bundle b = msg。getData(); 
  51.             String color = b。getString("color"); 
  52.             MyHandlerActivity。this。button。append(color); 
  53.  
  54.         } 
  55.     } 
  56.  
  57.     class MyThread implements Runnable { 
  58.         public void run() { 
  59.  
  60.             try { 
  61.                 Thread。sleep(10000); 
  62.             } catch (InterruptedException e) { 
  63.                 // TODO Auto-generated catch block 
  64.                 printStackTrace (); 
  65.             } 
  66.  
  67.             Log。d("thread。。。。。。。", "mThread。。。。。。。。"); 
  68.             Message msg = new Message(); 
  69.             Bundle b =  new  Bundle(); // store data 
  70.             b。putString("color", "我的"); 
  71.             msg。setData(b); 
  72.  
  73.             MyHandlerActivity. this . myHandler. sendMessage(msg);  // Send a message to the Handler to update the UI 
  74.  
  75.         } 
  76.     } 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325988171&siteId=291194637