`
四眼蛤蟆
  • 浏览: 97633 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Toast和Looper。Handler消息循环机制。

阅读更多

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

问题代码:

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,两个貌似联系不怎么紧密的类,却通过下面这个异常联系到了一起

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

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 ——>成员变量,在初始化时会跟着初始化

68    final Handler mHandler = new Handler();

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

 

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行到底做了什么,如下所示:

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 中寻根究底。

 

/**
 * 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 是正确但非正常的结果。

 

 

 

分享到:
评论
3 楼 hety163 2013-11-07  
受教了
2 楼 dcljava 2013-06-06  
分析的不错!
1 楼 cart55free99 2011-10-24  
很详细!!!

相关推荐

    android service thread toast handler

    Demo android组件Service与Toast 第二个版本

    toast和context 消息栏 消息提示 通知

    toast和context 消息栏 消息提示 通知

    Vue 之 Toast 消息提示插件的简单封装

    vue 中简单封装类似 Toast 的消息提示插件,然后在 vue 中任意位置都可以简单使用 Toast 消息显示最前,并且默认 3 秒后自动消失 实现原理 1、vue 创建 Toast 消失提示组件的显示界面 2、js 代码动态引入 Toast 的 ...

    Toast和Handler的间隔使用实例

    因为要让Toast长期显示,需要另外一个线程,每隔一个时间段,就循环显示一次。 先说明一下,本次需要用到Handle机制,因此不了解或者不熟悉Handle的同学,请先去看看Android上的Handle机制! 下面开始讲解代码实现...

    Bootstrap toast消息框插件

    bootstrap的toast消息框插件,弹出特效,前端用例,bootstrap 插件

    bootstrap-toast 的消息提示toast

    bootstrap-toast 的消息提示toastbootstrap-toast 的消息提示toastbootstrap-toast 的消息提示toastbootstrap-toast 的消息提示toastbootstrap-toast 的消息提示toastbootstrap-toast 的消息提示toast

    Toast消息框提示

    Toast各种弹跳消息框,方便各种需求的用户使用,文档仅供参考。多多谅解不足

    自定义Toast的显示内容和显示位置

    Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”一文中介绍了纯文本的Toast的使用,我们完全可以自定义Toast的显示内容和显示位置  要自定义...

    c# winform 类似android toast消息功能

    c# winform 类似android toast消息功能 超级强大,多个项目在用,可插拔,封装好了,拿去就可以用了

    Android 演示简单toast和带图片toast的实现方法.rar

    Android 演示简单toast和带图片toast的实现方法,这些toast在平时的Android应用开发中使用频繁,本源码演示了两种最实用toast的用法,一种是不带图片,另一种是带图片:  // 简单的toast,不带图片的实现方法:  ...

    012_android 之消息提示toast 和Context

    012_android 之消息提示toast 和Context视频教材,讲解的比较详细,有兴趣的可以学习下哦。

    android学习之toast和notification

    toast 和notification 的学习 很适合初学android的

    Toast 的消息提示方式 iToast.zip

    Toast 的消息提示方式 iToast ,iToast 实现了类似 Android Toast 的消息提示方式。

    Bootstrap3 Toast消息框插件

    Bootoast是一款Bootstrap3 Toast消息框插件。通过Bootoast插件你可以非常方便的制作Toast消息框效果,并且插件提供了各种参数来控制Toast消息框。

    bootstrap-toast 的消息提示垂直居中

    只需将属性修改成"positionClass": "toast-top-center", 代码如下//toastr提示框 toastr.options = { "closeButton": false, "debug": false, "newestOnTop": false, "progressBar": false, "positionClass...

    Android Toast hook方案,解决小米手机toast问题

    小米mimu系统,会对toast进行拦截,在用户提交的toast 消息体拼接一个前缀,由于该种会导致插件工程出现资源错乱,获取appLabel异常,现我们通过hook 动态代理,对消息发送做一个劫持,修改消息信息,还原原来的消息

    Android Handler 线程 示例代码

    实现进度条自动滚动实例,使用到了Handler,thread,toast,消息队列,事务队列等方法,通过按钮控制进度条的运行,使用toast来提示当前状态信息。

    Android 5.0以上Toast不显示的解决方法

    实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...

    消息提示Toast 列表组件ListView 实例示范

    1、使用Toast实现,点击2个不同按钮,分别显示两种不同效果的消息提示。 2、编写一个列表程序。实现点击不同项,显示对应消息提示。(列表组件ListView)。 运行效果参考:

Global site tag (gtag.js) - Google Analytics