zoukankan      html  css  js  c++  java
  • 关于android的UI更新机制与误区

    Android系统的消息队列和消息循环都是主线程的,其它后台服务等无法直接更新,必须通过下面的消息队列,由主线程的消息循环去依次执行更新ui;

    同时对于费时间超过5秒的事件,比如网络链接等,建议新开线程处理,否则系统会提示用户终止你。

    扩招知识:

    Android系统的消息队列和消息循环都是针对具体线程的,一个线程可以存在(当然也可以不存在)一个消息队列(Message Queue)和一个消息循环(Looper)。Android中除了UI线程(主线程),创建的工作线程默认是没有消息循环和消息队列的。如果想让该线程具有消息队列和消息循环,并具有消息处理机制,就需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。如以下代码所示:

    Java代码
    1. class LooperThread extends Thread {   
    2.       public Handler mHandler;   
    3.   
    4.       public void run() {   
    5.           Looper.prepare();   
    6.   
    7.           mHandler = new Handler() {   
    8.               public void handleMessage(Message msg) {   
    9.                   // process incoming messages here   
    10.               }   
    11.           };   
    12.   
    13.           Looper.loop();   
    14.       }   
    15.   }  

     
    1. class LooperThread extends Thread {  
    2.       public Handler mHandler;  
    3.   
    4.       public void run() {  
    5.           Looper.prepare();  
    6.   
    7.           mHandler = new Handler() {  
    8.               public void handleMessage(Message msg) {  
    9.                   // process incoming messages here  
    10.               }  
    11.           };  
    12.   
    13.           Looper.loop();  
    14.       }  
    15.   }  

           这样该线程就具有了消息处理机制了。如果不调用Looper.prepare()来创建消息队列,会报"java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"的错误。

           通过下图可以清晰显示出UI Thread, Worker Thread, Handler, Massage Queue, Looper之间的关系:

                              如上图:   UI Thread, Worker Thread, Handler, Massage Queue, Looper之间的关系

    上图中的几个基本概念:

           1.Message

           消息对象,顾名思义就是记录消息信息的类。这个类有几个比较重要的字段:

           a.arg1和arg2:我们可以使用两个字段用来存放我们需要传递的整型值,在Service中,我们可以用来存放Service的ID。

           b.obj:该字段是Object类型,我们可以让该字段传递某个多项到消息的接受者中。

           c.what:这个字段可以说是消息的标志,在消息处理中,我们可以根据这个字段的不同的值进行不同的处理,类似于我们在处理Button事件时,通过switch(v.getId())判断是点击了哪个按钮。

           在使用Message时,我们可以通过new Message()创建一个Message实例,但是Android更推荐我们通过Message.obtain()或者Handler.obtainMessage()获取Message对象。这并不一定是直接创建一个新的实例,而是先从消息池中看有没有可用的Message实例,存在则直接取出并返回这个实例。反之如果消息池中没有可用的Message实例,则根据给定的参数new一个新Message对象。通过分析源码可得知,Android系统默认情况下在消息池中实例化10个Message对象。

           2.MessageQueue

           消息队列,用来存放Message对象的数据结构,按照“先进先出”的原则存放消息。存放并非实际意义的保存,而是将Message对象以链表的方式串联起来的。MessageQueue对象不需要我们自己创建,而是有Looper对象对其进行管理,一个线程最多只可以拥有一个MessageQueue。我们可以通过Looper.myQueue()获取当前线程中的MessageQueue。

           3.Looper

           MessageQueue的管理者,在一个线程中,如果存在Looper对象,则必定存在MessageQueue对象,并且只存在一个Looper对象和一个MessageQueue对象。倘若我们的线程中存在Looper对象,则我们可以通过Looper.myLooper()获取,此外我们还可以通过Looper.getMainLooper()获取当前应用系统中主线程的Looper对象。在这个地方有一点需要注意,假如Looper对象位于应用程序主线程中,则Looper.myLooper()和Looper.getMainLooper()获取的是同一个对象。

           4.Handler

           消息的处理者。通过Handler对象我们可以封装Message对象,然后通过sendMessage(msg)把Message对象添加到MessageQueue中;当MessageQueue循环到该Message时,就会调用该Message对象对应的handler对象的handleMessage()方法对其进行处理。由于是在handleMessage()方法中处理消息,因此我们应该编写一个类继承自Handler,然后在handleMessage()处理我们需要的操作。

           另外,我们知道,Android UI操作并不是线程安全的,所以无法在子线程中更新UI。但Andriod提供了几种方法,可以在子线程中通知UI线程更新界面:

    • Activity.runOnUiThread( Runnable )
    • View.post( Runnable )
    • View.postDelayed( Runnable, long )
    • Handler

           比较常用的是通过Handler,用Handler来接收子线程发送的数据,并用此数据配合主线程更新UI。那么,只要在主线程中创建Handler对象,在子线程中调用Handler的sendMessage方法,就会把消息放入主线程的消息队列,并且将会在Handler主线程中调用该handler的handleMessage方法来处理消息。 

    1. package com.superonion;   
    2.   
    3. import android.app.Activity;   
    4. import android.os.Bundle;   
    5. import android.os.Message;   
    6. import android.util.Log;   
    7. import android.os.Handler;   
    8.   
    9. public class MyHandler extends Activity {   
    10.     static final String TAG = "Handler";   
    11.     Handler h = new Handler(){   
    12.         public void handleMessage (Message msg)   
    13.         {   
    14.             switch(msg.what)   
    15.             {   
    16.             case HANDLER_TEST:   
    17.                 Log.d(TAG, "The handler thread id = " + Thread.currentThread().getId() + " ");   
    18.                 break;   
    19.             }   
    20.         }   
    21.     };   
    22.   
    23.     static final int HANDLER_TEST = 1;   
    24.     /** Called when the activity is first created. */  
    25.     @Override  
    26.     public void onCreate(Bundle savedInstanceState) {   
    27.         super.onCreate(savedInstanceState);   
    28.         Log.d(TAG, "The main thread id = " + Thread.currentThread().getId() + " ");   
    29.   
    30.         new myThread().start();   
    31.         setContentView(R.layout.main);   
    32.     }   
    33.   
    34.     class myThread extends Thread   
    35.     {   
    36.         public void run()   
    37.         {   
    38.             Message msg = new Message();   
    39.             msg.what = HANDLER_TEST;   
    40.             h.sendMessage(msg);   
    41.             Log.d(TAG, "The worker thread id = " + Thread.currentThread().getId() + " ");   
    42.         }   
    43.     }   
    44. }  

     
    1. package com.superonion;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.os.Message;  
    6. import android.util.Log;  
    7. import android.os.Handler;  
    8.   
    9. public class MyHandler extends Activity {  
    10.     static final String TAG = "Handler";  
    11.     Handler h = new Handler(){  
    12.         public void handleMessage (Message msg)  
    13.         {  
    14.             switch(msg.what)  
    15.             {  
    16.             case HANDLER_TEST:  
    17.                 Log.d(TAG, "The handler thread id = " + Thread.currentThread().getId() + " ");  
    18.                 break;  
    19.             }  
    20.         }  
    21.     };  
    22.   
    23.     static final int HANDLER_TEST = 1;  
    24.     /** Called when the activity is first created. */  
    25.     @Override  
    26.     public void onCreate(Bundle savedInstanceState) {  
    27.         super.onCreate(savedInstanceState);  
    28.         Log.d(TAG, "The main thread id = " + Thread.currentThread().getId() + " ");  
    29.   
    30.         new myThread().start();  
    31.         setContentView(R.layout.main);  
    32.     }  
    33.   
    34.     class myThread extends Thread  
    35.     {  
    36.         public void run()  
    37.         {  
    38.             Message msg = new Message();  
    39.             msg.what = HANDLER_TEST;  
    40.             h.sendMessage(msg);  
    41.             Log.d(TAG, "The worker thread id = " + Thread.currentThread().getId() + " ");  
    42.         }  
    43.     }  
    44. }  

           以上代码中,Handler在主线程中创建后,子线程通过sendMessage()方法就可以将消息发送到主线程中,并在handleMessage()方法中处理。

    本着资源共享的原则,欢迎各位朋友在此基础上完善,并进一步分享,让我们的实现更加优雅。如果有任何疑问和需要进一步交流可以留言沟通 Testner创始人(testner.club) Sea
  • 相关阅读:
    $.getJSON()
    seconds
    ini_set
    validation
    component
    ini_set();
    长期阅读英文技术博客的好处
    用xml还是json
    单​手​打​字
    洛谷P1141 01迷宫
  • 原文地址:https://www.cnblogs.com/sea520/p/4576295.html
Copyright © 2011-2022 走看看