一个小Demo,无意间,在子线程中Toast了一把,竟然报错了
因为Toast在service和activity中都可以执行。所以开始就认为和ui线程没有有太大的关系,而现在子线程Toast竟然报错!无奈之下,花了半天的时间看了一下Handler,Looper,Toast的源码,终于搞定了。(这个效率..本人愚钝啊)----->的确和UI线程没有关系
记录下来,希望对遇上同样问题的同学有所帮助。下面正题
1、错误的关键位置在于Toast初始化的时候,这句
public class Toast {final Handler mHandler = new Handler();….}
2、其实在别的地方也看到过,普通线程不能直接new一个Handler
原因:
1
2
3
4
5
6
7
8
9
10
11
12
|
public
Handler(){
mLooper = Looper.myLooper();
if
(mLooper ==
null
) {
throw
new
RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()"
);
}
}
|
3、而Looper中
1
2
3
4
|
public
static
final
Looper myLooper() {
//这个方法是从当前线程的ThreadLocal中拿出设置的looper
return
(Looper)sThreadLocal.get();
}
|
而事实上子线程只是一个普通的线程,其ThreadLoacl中没有设置过Looper,所以会抛出异常
4、解决方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
void
onClick(View v) {
new
Thread(){
public
void
run() {
Log.i(
"log"
,
"run"
);
Looper.prepare();
Toast.makeText(ActivityTestActivity.
this
,
"toast"
,
1
).show();
Looper.loop();
// 进入loop中的循环,查看消息队列
};
}.start();
}
|
Looper.prepare()方法参考
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//Looper
public
static
final
void
prepare() {
if
(sThreadLocal.get() !=
null
) {
throw
new
RuntimeException(
"Only one Looper may be created per thread"
);
}
sThreadLocal.set(
new
Looper());
}
|
下边的可以忽略了
5、至于内部的通讯机制就不知道了
只知道show()方法里边调用了InotificationManager. enqueueToast(pkgName, tn, mDuration)
其中tn是继承了ItransientNotification.Stub的远程通信接口,而handler也是在这个TN类中调用!猜想内部机制也是NotificationService的进程间通信机制!
下边代码,算是管中窥豹吧
-----源码不是这个样子的,被我概括
ItransientNotification中有个show方法
1
2
3
4
5
6
|
public
void
show() {
if
(localLOGV) Log.v(TAG,
"SHOW: "
+
this
);
Handler.post(mShow);
}
|
其中mshow是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
final
Runnable mShow =
new
Runnable() {
public
void
run() {
…..
WindowManagerImpl mWM = WindowManagerImpl.getDefault();
mWM.addView(mView, mParams);
…..
}
};
|