1、使用Thread+Handler实现非UI线程更新UI界面
private MyHandler mHandler = new MyHandler();
......
mHandler.sendResult(MyHandler.UPDATE_VIEW, null);
......
private class MyHandler extends Handler{
private static final int UPDATE_VIEW = 0;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_VIEW:
updateView();
break;
default:
break;
}
}
private void sendResult(int result, Object obj) {
Message message = new Message();
message.what = result;
message.obj = obj;
sendMessage(message);
//obtainMessage(result, obj).sendToTarget(); //效率更高
}
}
2、使用View的post方法来更新UI
mView.post(new Runnable() {
@Override
public void run() {
updateView();
}
});
Android在非UI线程中更新UI的方法,布布扣,bubuko.com
Android在非UI线程中更新UI的方法