Can't create handler inside thread that has not called Looper.prepare()。
1、在主线程(UI线程)里,如果创建Handler时不传入Looper对象,那么将直接使用主线程的Looper对象(系统已经帮我们创建了);
2、在子线程(工作线程)里,如果创建Handler时不传入Looper对象,那么,这个Handler将不能接收处理消息。
Looper looper1 = Looper.getMainLooper();
Looper looper2 = activity.getMainLooper();
System.out.println("looper1 是否等于 looper2?" + (looper1 == looper2)); // 输出为true
Handler在主线程和子线程实例化的区别?new Handler()和new Handler(Looper.getMainLooper())的区别
Handler handler = new Handler();这个会默认用当前线程的looper
一般而言,如果你的Handler是要来刷新操作UI的,那么就需要在主线程下跑。
情况:
1.要刷新UI,handler要用到主线程的looper。
a)在主线程 使用
Handler handler = new Handler();
b)如果在其他线程,也要满足这个功能的话,使用
Handler handler = new Handler(Looper.getMainLooper());
2.不用刷新ui,只是处理消息。
a)当前线程如果是主线程的话,
使用Handler handler = new Handler();
b)不是主线程的话,
Looper.prepare();
Handler handler = new Handler();
Looper.loop();
或者
Handler handler = new Handler(Looper.getMainLooper());
若是实例化的时候用Looper.getMainLooper()就表示放到主UI线程去处理。
如果不是的话,因为只有UI线程默认Loop.prepare();Loop.loop();过,其他线程需要手动调用这两个,否则会报错。
有人会问:那我在onclick事件,new了一个类,那么类对象和属性,是在主线程还是子线程运行?
答:UI线程
最近做项目时出现个问题。在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下
this.mThirdHandler = new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
//isStop为基类中的一个私有成员
isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());
};
};
但不知道为啥一直报错:Can't create handler inside thread that has not called Looper.prepare()。
搜索后发现,原因是此Handler没有Looper。到哪儿去找Looper呢?自己建?
在代码前加入Looper.prepare();,心想这回可以了吧?
没想到依然报错,错误显示,一个主进程只能有一个Looper,要死了。郁闷中...
突然我想到主进程中肯定有Looper,Context.getMainLooper(),再看Handler的实例化时是可以指定Looper的,太爽了,最后代码如下
this.mThirdHandler = new Handler(mContext.getMainLooper()){
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());
};
};
mContext为主界面context,实例化基类时引入的一个参数。
希望以上的方法,可以给大家带来点帮助。
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case ID_USER:
//获取传递的数据
//Bundle data = msg.getData();
//int count = data.getInt("COUNT");
//处理UI更新等操作
}
};
};
只要在run()方法中加入 Looper.prepare()就可以解决问题了,但是出现这种问题的原因是因为Android中不能在子线程中来刷新UI线程。建议是在你的子线程中添加hander来发送消息更新线程。
在主activity中定一个Handler的成员,然后实现handlemassage函数,创建线程后在runable的run函数里new一个message,然后指定message对象的what成员,这个是指定message的一个id,然后在run中调用Handler的成员,使用其成员方法中的sendmessage,handlemassage函数中参数有个massage,根据该message参数中的what来对你发送message时指定的what来处理UI的功能
主activity中创建线程
- MyThread thread = new MyThread();
- Thread mThread = new Thread(thread);
- mThread.start();
MyThread
- class MyThread implements Runnable {
- public void run() {
- //执行数据操作,不涉及到UI
- Message msg = new Message();
- msg.what = ID_USER;
- //这三句可以传递数据
- // Bundle data = new Bundle();
- // data.putInt("COUNT", 100);//COUNT是标签,handleMessage中使用
- // msg.setData(data);
- mHandler.sendMessage(msg); // 向Handler发送消息,更新UI
- }
通过一个Handler来处理这些,可以在一个线程的run方法中调用handler对象的 postMessage或sendMessage方法来实现,Android程序内部维护着一个消息队列,会轮训处理这些。
Looper又是什么呢? ,其实Android中每一个Thread都跟着一个Looper,Looper可以帮助Thread维护一个消息队列,但是Looper和Handler没有什么关系,我们从开源的代码可以看到Android还提供了一个Thread继承类HanderThread可以帮助我们处理,在HandlerThread对象中可以通过getLooper方法获取一个Looper对象控制句柄,我们可以将其这个Looper对象映射到一个Handler中去来实现一个线程同步机制,Looper对象的执行需要初始化Looper.prepare方法就能解决上面提到的问题。
Message 在Android是什么呢? 对于Android中Handler可以传递一些内容,通过Bundle对象可以封装String、Integer以及Blob二进制对象,我们通过在线程中使用Handler对象的 sendEmptyMessage或sendMessage方法来传递一个Bundle对象到Handler处理器。对于Handler类提供了重写方法handleMessage(Message msg) 来判断,通过msg.what来区分每条信息。将Bundle解包来实现Handler类更新UI线程中的内容实现控件的刷新操作。相关的Handler对象有关消息发送sendXXXX相关方法如下,同时还有postXXXX相关方法,一个为发送后直接返回,一个为处理后才返回
深入理解Android消息处理系统——Looper、Handler、Thread
Android Looper和Handler
Message:消息,其中包含了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理。
Handler:处理者,负责Message的发送及处理。使用Handler时,需要实现handleMessage(Message msg)方法来对特定的Message进行处理,例如更新UI等。
MessageQueue:消息队列,用来存放Handler发送过来的消息,并按照FIFO规则执行。当然,存放Message并非实际意义的保存,而是将Message以链表的方式串联起来的,等待Looper的抽取。
Looper:消息泵,不断地从MessageQueue中抽取Message执行。因此,一个MessageQueue需要一个Looper。
Thread:线程,负责调度整个消息循环,即消息循环的执行场所。
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();//使消息循环起作用,从消息队列里取消息,处理消息
}
}
注:写在Looper.loop()之后的代码不会被立即执行,当调用后 mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。Looper对象通过MessageQueue 来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。
这样你的线程就具有了消息处理机制了,在Handler中进行消息处理。
Activity是一个UI线程,运行于主线程中,Android系统在启动的时候会为Activity创建一个消息队列和消息循环(Looper)。详细实现请参考ActivityThread.java文件。
......
public static final void main(String[] args) {
......
Looper.prepareMainLooper();
......
ActivityThread thread = new ActivityThread();
thread.attach(false);
......
Looper.loop();
......
thread.detach();
......
}
}
101
102
103
104
105
106
115
116
117
118
119
120
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
生成
发送
在Handler.java的sendMessageAtTime(Message msg, long uptimeMillis)方法中,我们看到,它找到它所引用的MessageQueue,然后将Message的target设定成自己(目的是为了在处理消息环节,Message能找到正确的Handler),再将这个Message纳入到消息队列中。
抽取
在Looper.java的loop()函数里,我们看到,这里有一个死循环,不断地从MessageQueue中获取下一个(next方法)Message,然后通过Message中携带的target信息,交由正确的Handler处理(dispatchMessage方法)。
处理
在Handler.java的dispatchMessage(Message msg)方法里,其中的一个分支就是调用handleMessage方法来处理这条Message,而这也正是我们在职责处描述使用Handler时需要实现handleMessage(Message msg)的原因。
至于dispatchMessage方法中的另外一个分支,我将会在后面的内容中说明。
至此,我们看到,一个Message经由Handler的发送,MessageQueue的入队,Looper的抽取,又再一次地回到Handler的怀抱。而绕的这一圈,也正好帮助我们将同步操作变成了异步操作。
Handler的作用是把消息加入特定的(Looper)消息队列中,并分发和处理该消息队列中的消息。构造Handler的时候可以指定一个Looper对象,如果不指定则利用当前线程的Looper创建。详细实现请参考Looper的源码。一个Activity中可以创建多个工作线程或者其他的组件,如果这些线程或者组件把他们的消息放入Activity的主线程消息队列,那么该消息就会在 主线程中处理了。因为主线程一般负责界面的更新操作,并且Android系统中的weget不是线程安全的,所以这种方式可以很好的实现Android界 面更新。在Android系统中这种方式有着广泛的运用。 那么另外一个线程怎样把消息放入主线程的消息队列呢?答案是通过Handle对象,只要Handler对象以主线程的Looper创建,那么调用 Handler的sendMessage等接口,将会把消息放入队列都将是放入主线程的消息队列。并且将会在Handler主线程中调用该handler 的handleMessage接口来处理消息。
3)剩下的部分,我们将讨论一下Handler所处的线程及更新UI的方式。
在主线程(即UI线程)里,如果创建Handler时不传入Looper对象,那么将直接使用主线程的Looper对象(系统已经帮我们创建了);
在子线程(工作线程)里,如果创建Handler时不传入Looper对象,那么,这个Handler将不能接收处理消息。
在这种情况下,通用的作法是:
在创建Handler之前,为该线程准备好一个Looper(Looper.prepare),然后让这个Looper跑起来(Looper.loop),抽取Message,这样,Handler才能正常工作。
因此,Handler处理消息总是在创建Handler的线程里运行。而我们的消息处理中,不乏更新UI的操作,不正确的线程直接更新UI将引发异常。因此,需要时刻关心Handler在哪个线程里创建的。
如何更新UI才能不出异常呢?SDK告诉我们,有以下4种方式可以从其它线程访问UI线程:
·
·
·
其中,重点说一下的是View.post(Runnable)方法。在post(Runnable action)方法里,View获得当前线程(即UI线程)的Handler,然后将action对象post到Handler里。在Handler里,它将传递过来的action对象包装成一个Message(Message的callback为action),然后将其投入UI线程的消息循环中。在Handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法。而此时,已经路由到UI线程里,因此,我们可以毫无顾虑的来更新UI。
4) 几点小结
·
·
·
·
·
通过上面的分析,我们可以得出如下结论:
1、如果通过工作线程刷新界面,推荐使用handler对象来实现。
2、注意工作线程和主线程之间的竞争关系。推荐handler对象在主线程中构造完成(并且启动工作线程之后不要再修改之,否则会出现数据不一致),然后在工作线程中可以放心的调用发送消息SendMessage等接口。
3、除了2所述的hanlder对象之外的任何主线程的成员变量如果在工作线程中调用,仔细考虑线程同步问题。如果有必要需要加入同步对象保护该变量。
4、handler对象的handleMessage接口将会在主线程中调用。在这个函数可以放心的调用主线程中任何变量和函数,进而完成更新UI的任务。
5、Android很多API也利用Handler这种线程特性,作为一种回调函数的变种,来通知调用者。这样Android框架就可以在其线程中将消息发送到调用者的线程消息队列之中,不用担心线程同步的问题。