Toast要注意的地方

一个小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);
 
             …..
 
     }
 
  };

文章出处:http://blog.csdn.net/boliu123/article/details/6930451

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值