笔记39 | Android线程之Looper.prepare()+Looper.loop()和HandlerThread()

来源:互联网 发布:收银机什么软件好 编辑:程序博客网 时间:2024/04/30 23:24

地址

http://www.jianshu.com/p/dc2a4fb0fcde


目录

  • Looper概念
  • Looper实例
  • HandlerThread概念
  • HandlerThread实例

Looper概念

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发送消息即可。


Looper实例:

  • 代码
    private void initThead() {        new Thread(new Runnable() {            public void run() {                Looper.prepare();//启用Looper。                 handler1 = new Handler(){                    @Override                    public void handleMessage(Message msg) {                        super.handleMessage(msg);                        Log.i("md",  "threadName--:" + Thread.currentThread().getName() + "  ,messageWhat-:"+ msg.what );                                   }                };                          handler1.sendEmptyMessage( 5 ) ;                Looper.loop();//让Looper开始工作,从消息队列里取消息,处理消息,让消息处理在该线程中完成。                Log.i("md",  "threadName--:" + Thread.currentThread().getName());//代码不执行            }        }).start();    }  ```- 打印```php01-01 00:19:35.104: I/md(2068): threadName--:Thread-168  ,messageWhat-:501-01 00:19:35.116: I/md(2068): threadName--:Thread-169  ,messageWhat-:2<div class="se-preview-section-delimiter"></div>

然而这一切都可以用HandlerThread类来帮我们做这些逻辑操作。


HandlerThread概念

HandlerThread本质上就是一个普通Thread,只不过内部建立了Looper.

/** * Handy class for starting a new thread that has a looper. The looper can then be  * used to create handler classes. Note that start() must still be called. */public class HandlerThread extends Thread {... @Override    public void run() {        mTid = Process.myTid();        Looper.prepare();        synchronized (this) {            mLooper = Looper.myLooper();            notifyAll();        }        Process.setThreadPriority(mPriority);        onLooperPrepared();        Looper.loop();        mTid = -1; ...   }}<div class="se-preview-section-delimiter"></div>

那么这个Handler对象就是与HandlerThread这个线程绑定了(这时就不再是与UI线程绑定了,这样它处理耗时操作将不会阻塞UI)。


 HandlerThread实例

  • 代码
private void initHandlerThread() {        HandlerThread threadHandlerThread = new HandlerThread("Handler-thread");        .start();         handler3 = new Handler(threadHandlerThread.getLooper()){            @Override            public void handleMessage(Message msg) {                // TODO Auto-generated method stub                super.handleMessage(msg);                Log.i("md",  "threadName--:" + Thread.currentThread().getName() + "  ,messageWhat-:"+ msg.what );            }        };        handler3.sendEmptyMessage(2);        new Thread(new Runnable() {            @Override            public void run() {                handler3.sendEmptyMessage(5);            }        }).start();    }<div class="se-preview-section-delimiter"></div>
  • 退出HandlerThread
 threadHandlerThread.quit() ; //释放资源<div class="se-preview-section-delimiter"></div>
  • 打印
01-01 01:28:56.405: I/md(2418): threadName--:Handler-thread  ,messageWhat-:201-01 01:28:56.415: I/md(2418): threadName--:Handler-thread  ,messageWhat-:5

阅读全文
0 0
原创粉丝点击