android-86-Can't create handler inside thread that has not called Looper.prepare()

以下是Android API中的一个典型的Looper thread实现:

//Handler不带参数的默认构造函数:new Handler(),实际上是通过Looper.myLooper()来获取当前线程中的消息循环,
//而默认情况下,线程是没有消息循环的,所以要调用 Looper.prepare()来给线程创建消息循环,然后再通过,Looper.loop()来使消息循环起作用。


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

另,Activity的MainUI线程默认是有消息队列的。所以在Activity中新建Handler时,不需要先调用Looper.prepare()。


http://blog.163.com/hqq133@126/blog/static/168747811201162041044112/

posted @ 2015-08-04 17:52  瘋耔  阅读(215)  评论(0编辑  收藏  举报
跳至侧栏