关于Handler实例化的一些关键信息,具体如下:
-
在主线程中可以直接创建Handler对象,而在子线程中需要先调用Looper.prepare()才能创建Handler对象,否则运行抛出”Can’t create handler inside thread that has not called Looper.prepare()”异常信息。
-
每个线程中最多只能有一个Looper对象,否则抛出异常。
-
可以通过Looper.myLooper()获取当前线程的Looper实例,通过Looper.getMainLooper()获取主(UI)线程的Looper实例。
-
一个Looper只能对应了一个MessageQueue。
-
一个线程中只有一个Looper实例,一个MessageQueue实例,可以有多个Handler实例