请问为什么getMainLooper和主线程通信失败
现有如下代码:main.java
public class startHandler extends Handler
{
public startHandler() {
super();
}
public startHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
Log.i(Unity.TAG, "END!");
textView.setText("End!");
default:
break;
}
super.handleMessage(msg);
}
}
thread.java
class calcHandler extends Handler
{
public calcHandler() {
// TODO Auto-generated constructor stub
super(Looper.myLooper());
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
Log.i(Unity.TAG, "exit!");
Handler mainHandler = new Handler(Looper.getMainLooper());
Message newMsg = mainHandler.obtainMessage();
newMsg.what = 1;
mainHandler.sendMessage(newMsg);
}
}
super.handleMessage(msg);
}
}
在子线程thread.java文件中开启消息循环,一段时间后通过getMainLooper获取主线程Looper向其发送消息更新UI,但是调试的时候发现thread.java中的mainHandler.sendMessage(newMsg);被调用,但是主线程中handleMessage()方法并未被调用,这是什么原因呢?本人新手,谢谢! looper handler --------------------编程问答-------------------- Handler mainHandler = new Handler(Looper.getMainLooper());
改成
Handler mainHandler = new startHandler(Looper.getMainLooper());
--------------------编程问答--------------------
这样会报错,startHandler是Main的内部类。
而且我在主线程中也使用了new handler()来获取子线程looper的handler,消息是可以从主线程发送到子线程的
补充:移动开发 , Android