zoukankan      html  css  js  c++  java
  • Android -- Looper.prepare()和Looper.loop() —深入版

    Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理。handler其实可以看做是一个工具类,用来向消息队列中插入消息的。

        (1) Looper类用来为一个线程开启一个消息循环。     默认情况下android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环。)     Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。 

    (2) 通常是通过Handler对象来与Looper进行交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。     默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,Handler在主线程中定义,那么它是与主线程的Looper绑定。 mainHandler = new Handler() 等价于new Handler(Looper.myLooper()). Looper.myLooper():获取当前进程的looper对象,类似的 Looper.getMainLooper() 用于获取主线程的Looper对象。 

    (3) 在非主线程中直接new Handler() 会报如下的错误: E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。 

    (4) Looper.loop(); 让Looper开始工作,从消息队列里取消息,处理消息。 

        注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。
    (5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。 
        把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。         Android官方文档中Looper的介绍: Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.
    Most interaction with a message loop is through the Handler class. 

    This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

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

    如果线程中使用Looper.prepare()和Looper.loop()创建了消息队列就可以让消息处理在该线程中完成。

     android HandlerThread使用小例

    之前研究过handler 和 looper 消息队列,不过android里的handler不是另外开启线程来执行的,还是在主UI线程中,如果想另启线程的话需要用到HandlerThread来实现。在使用HandlerThread的时候需要实现CallBack接口以重写handlerMessage方法,在handlerMessage方法中来处理自己的逻辑。下来给出一个小例子程序。

    layout文件很简单,就一个按钮来启动HanlderTread线程

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <TextView  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:text="@string/hello" />  
    11.   
    12.     <Button  
    13.         android:id="@+id/handlerThreadBtn"  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.         android:text="startHandlerThread" />  
    17.   
    18. </LinearLayout>  
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
        <Button
            android:id="@+id/handlerThreadBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startHandlerThread" />
    </LinearLayout>

    Activity代码如下:

    1. package com.tayue;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.os.Handler;  
    6. import android.os.Handler.Callback;  
    7. import android.os.HandlerThread;  
    8. import android.os.Message;  
    9. import android.view.View;  
    10. import android.view.View.OnClickListener;  
    11. import android.widget.Button;  
    12. /** 
    13.  *  
    14.  * @author xionglei 
    15.  * 
    16.  */  
    17. public class TestHandlerActivity extends Activity implements OnClickListener{  
    18.       
    19.     public Button handlerThreadBTN;   
    20.     MyHandlerThread handlerThread;  
    21.     Handler handler;  
    22.       
    23.     /** Called when the activity is first created. */  
    24.     @Override  
    25.     public void onCreate(Bundle savedInstanceState) {  
    26.         super.onCreate(savedInstanceState);  
    27.         //打印UI线程的名称   
    28.         System.out.println("onCreate  CurrentThread = " + Thread.currentThread().getName());  
    29.           
    30.         setContentView(R.layout.main);  
    31.           
    32.         handlerThreadBTN = (Button) findViewById(R.id.handlerThreadBtn);  
    33.         handlerThreadBTN.setOnClickListener(this);  
    34.           
    35.         handlerThread = new MyHandlerThread("myHanler");  
    36.         handlerThread.start();  
    37.         //注意: 这里必须用到handler的这个构造器,因为需要把callback传进去,从而使自己的HandlerThread的handlerMessage来替换掉Handler原生的handlerThread   
    38.         handler = new Handler(handlerThread.getLooper(), handlerThread);         
    39.     }  
    40.   
    41.     @Override  
    42.     public void onClick(View v) {  
    43.         //点击按钮后来开启线程   
    44.         handler.sendEmptyMessage(1);  
    45.     }      
    46.       
    47.     private class MyHandlerThread extends HandlerThread implements Callback {  
    48.   
    49.         public MyHandlerThread(String name) {  
    50.             super(name);  
    51.         }  
    52.   
    53.         @Override  
    54.         public boolean handleMessage(Message msg) {  
    55.             //打印线程的名称   
    56.             System.out.println(" handleMessage CurrentThread = " + Thread.currentThread().getName());  
    57.             return true;  
    58.         }              
    59.     }           
    60. }  
    package com.tayue;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Handler.Callback;
    import android.os.HandlerThread;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    /**
     * 
     * @author xionglei
     *
     */
    public class TestHandlerActivity extends Activity implements OnClickListener{
        
        public Button handlerThreadBTN; 
        MyHandlerThread handlerThread;
        Handler handler;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //打印UI线程的名称
            System.out.println("onCreate  CurrentThread = " + Thread.currentThread().getName());
            
            setContentView(R.layout.main);
            
            handlerThreadBTN = (Button) findViewById(R.id.handlerThreadBtn);
            handlerThreadBTN.setOnClickListener(this);
            
            handlerThread = new MyHandlerThread("myHanler");
            handlerThread.start();
            //注意: 这里必须用到handler的这个构造器,因为需要把callback传进去,从而使自己的HandlerThread的handlerMessage来替换掉Handler原生的handlerThread
            handler = new Handler(handlerThread.getLooper(), handlerThread);       
        }
    
        @Override
        public void onClick(View v) {
            //点击按钮后来开启线程
            handler.sendEmptyMessage(1);
        }    
        
        private class MyHandlerThread extends HandlerThread implements Callback {
    
            public MyHandlerThread(String name) {
                super(name);
            }
    
            @Override
            public boolean handleMessage(Message msg) {
                //打印线程的名称
                System.out.println(" handleMessage CurrentThread = " + Thread.currentThread().getName());
                return true;
            }            
        }         
    }

    点击按钮,打印的日志如下(这里点击了3次) 07-06 09:32:48.776: I/System.out(780): onCreate  CurrentThread = main 07-06 09:32:55.076: I/System.out(780):  handleMessage CurrentThread = myHanler 07-06 09:32:58.669: I/System.out(780):  handleMessage CurrentThread = myHanler 07-06 09:33:03.476: I/System.out(780):  handleMessage CurrentThread = myHanler

    HandlerThread就这么简单。

    当然 android自己也有异步线程的handler,就是AsyncTask,这个类就是封装了HandlerThread 和handler来实现异步多线程的操作的。

    同样可以这样使用:

    1. private boolean iscancel = false; //用户手动取消登录的标志位   
    2.   
    3.     handlerThread = new HandlerThread("myHandlerThread");  
    4.                     handlerThread.start();  
    5.                     handler = new MyHandler(handlerThread.getLooper());  
    6.                 // 将要执行的线程对象添加到线程队列中   
    7.                         handler.post(new Runnable() {  
    8.                             @Override  
    9.                             public void run() {  
    10.                                 Message message = handler.obtainMessage();  
    11.                                 UserBean user = Bbs.getInstance().Login(username, password);//耗时任务   
    12.                                 Bundle b = new Bundle();  
    13.                                 b.putSerializable("user", user);  
    14.                                 message.setData(b);  
    15.                                 message.sendToTarget(); //或使用 handler.sendMessage(message);   
    16.                             }  
    17.                         });  
    18.     class MyHandler extends Handler {  
    19.   
    20.             public MyHandler(Looper looper) {  
    21.                 super(looper);  
    22.             }  
    23.   
    24.             @Override  
    25.             public void handleMessage(Message msg) {  
    26.                 if(iscancel == false){  
    27.                     // 操作UI线程的代码   
    28.                     Bundle b = msg.getData();  
    29.                     UserBean user = (UserBean)b.get("user");  
    30.                                      ......  
    31.                }  
    32.            }  
    33.     }    
    private boolean iscancel = false; //用户手动取消登录的标志位
    
        handlerThread = new HandlerThread("myHandlerThread");
                        handlerThread.start();
                        handler = new MyHandler(handlerThread.getLooper());
                    // 将要执行的线程对象添加到线程队列中
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Message message = handler.obtainMessage();
                                    UserBean user = Bbs.getInstance().Login(username, password);//耗时任务
                                    Bundle b = new Bundle();
                                    b.putSerializable("user", user);
                                    message.setData(b);
                                    message.sendToTarget(); //或使用 handler.sendMessage(message);
                                }
                            });
        class MyHandler extends Handler {
    
                public MyHandler(Looper looper) {
                    super(looper);
                }
    
                @Override
                public void handleMessage(Message msg) {
                    if(iscancel == false){
                        // 操作UI线程的代码
                        Bundle b = msg.getData();
                        UserBean user = (UserBean)b.get("user");
                                         ......
                   }
               }
        }  
  • 相关阅读:
    linux(centos)搭建SVN服务器
    应该具备的能力
    Oracle trunc()函数的用法
    Realistic View for Autodesk Revit 2021
    Snowman
    A Material-Texture Painting Tool
    A Color Picker based on manifold learning
    CPU Path Tracing Renderer
    Rig Space FEM Simulation
    MPM Snow Simulation
  • 原文地址:https://www.cnblogs.com/zhongle/p/3915077.html
Copyright © 2011-2022 走看看