您的位置:首页 > 移动开发 > Android开发

【Android】Toast和Looper。Handler消息循环机制

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

(2) 通常是通过Handler对象来与Looper交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,在主线程中定义,其是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).
Looper.myLooper():Return the Looper object associated with the current thread 获取当前进程的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) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。

Toast或者Dialog中都有一个Handler的成员变量,在初始化时都会跟着初始化,而Toast或者Dialog中的Handler都需要一个Looper,所以需要在包含该Toast或者Dialog的线程中(如下面的Timer线程)初始化Looper。Looper.prepare();

问题代码:

Java代码







private Handler myHandler = new Handler() { public void handleMessage(Message msg) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { InputMethodManager m = (InputMethodManager) editText .getContext().getSystemService( Context.INPUT_METHOD_SERVICE); m.showSoftInput(editText, 0); // Looper.prepare(); Toast.makeText(Main.this, "show", Toast.LENGTH_LONG).show(); Looper.loop(); } }, 1000); } }

private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager m = (InputMethodManager) editText
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
m.showSoftInput(editText, 0);
//
Looper.prepare();
Toast.makeText(Main.this, "show", Toast.LENGTH_LONG).show();
Looper.loop();
}
}, 1000);
}
}


Toast 和 Looper,一个属于 android.widget,一个属于 android.os,两个貌似联系不怎么紧密的类,却通过下面这个异常联系到了一起:

Java代码







E/AndroidRuntime( 1819): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() E/AndroidRuntime( 1819): at android.os.Handler.<init>(Handler.java:121) E/AndroidRuntime( 1819): at android.widget.Toast.<init>(Toast.java:68) E/AndroidRuntime( 1819): at android.widget.Toast.makeText(Toast.java:231)
E/AndroidRuntime( 1819): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
E/AndroidRuntime( 1819):        at android.os.Handler.<init>(Handler.java:121)
E/AndroidRuntime( 1819):        at android.widget.Toast.<init>(Toast.java:68)
E/AndroidRuntime( 1819):        at android.widget.Toast.makeText(Toast.java:231)


Handler.java:121

Java代码







119 mLooper = Looper.myLooper(); 120 if (mLooper == null) { 121 throw new RuntimeException( 122 "Can't create handler inside thread that has not called Looper.prepare()");}

119        mLooper = Looper.myLooper();
120        if (mLooper == null) {
121            throw new RuntimeException(
122            "Can't create handler inside thread that has not called Looper.prepare()");}


Toast.java:68 ——>成员变量,在初始化时会跟着初始化

Java代码







68 final Handler mHandler = new Handler();

68    final Handler mHandler = new Handler();

由以上的错误信息可以看出:程序要创建 handler,但是发现Looper.prepare还没有被调用。通过 Android SDK 中的Reference可以看到,Looper、Handler 的调用是非常有讲究的,如下面示例代码:

Java代码







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(); } }

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();
}
}

言归正题,继续寻找 Toast、Looper 和 Handler 三者之间的联系,也许谜底就能解开了。欲揭谜底,从源码入手是一条捷径。

Toast.java 的第231行的代码是创建一个新的Toast实例,而实例化的过程中,就需要执行第68行,也就是声明并创建Handler(成员变量)的实例。那么来看Handler.java的第121行到底做了什么,如下所示:

Java代码







119 mLooper = Looper.myLooper(); 120 if (mLooper == null) { 121 throw new RuntimeException( 122 "Can't create handler inside thread that has not called Looper.prepare()");}

119        mLooper = Looper.myLooper();
120        if (mLooper == null) {
121            throw new RuntimeException(
122            "Can't create handler inside thread that has not called Looper.prepare()");}

到此,距离真相的解开近了一大步,既然抛出了 RuntimeException,那么 mLooper 肯定是 null,但是为什么 Looper.myLooper() 会返回 null?继续进入到 Looper.java 中寻根究底。

Java代码







/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static final Looper myLooper() { return (Looper)sThreadLocal.get(); }

/**
* Return the Looper object associated with the current thread.  Returns
* null if the calling thread is not associated with a Looper.
*/
public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}

以上就是 myLooper() 方法的真实面貌,通过注释可以看出问题的真正原因在于当前线程并没有绑定 Looper,返回为 null 是正确但非正常的结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: