Android异步消息处理机制详解

来源:互联网 发布:淘宝怎么改会员名字 编辑:程序博客网 时间:2024/05/29 09:07

关于Handler实例化的一些关键信息,具体如下:

  1. 在主线程中可以直接创建Handler对象,而在子线程中需要先调用Looper.prepare()才能创建Handler对象,否则运行抛出”Can’t create handler inside thread that has not called Looper.prepare()”异常信息。

  2. 每个线程中最多只能有一个Looper对象,否则抛出异常。

  3. 可以通过Looper.myLooper()获取当前线程的Looper实例,通过Looper.getMainLooper()获取主(UI)线程的Looper实例。

  4. 一个Looper只能对应了一个MessageQueue。

  5. 一个线程中只有一个Looper实例,一个MessageQueue实例,可以有多个Handler实例
0 0
原创粉丝点击