Handler是Android消息传递的一种机制,可用于在子线程做更新UI(实际上还是在UI线程中更新),与Handler相关联的类有Looper,Message等。Handler的方法有post() sendmessage()等。
子线程更新UI的方式:
1) handler.post(new Runnable())方法。
注意:这个Handler必须为主线程的handler才能更新UI;
2) handler.sentMessage()方法
同上,handler必须为主线程hander。主线程回调 handler.handlerMessage()方法来更新UI。最常见。
3) runOnUiThread(new Runnable())方法。
4) view.post(new Runnable()) 方法
总结下就是:Handler封装了消息的发送,并把消息发送给自己。所以上述四个思路都是子线程拿到主线程的Handler,重写他的发送消息方法,主线程中重写handleMessage方法,这样就可以在“子线程”中修改UI。