Handler的sendMessage和postDelay的区别
2021-04-24 本文已影响0人
挖打发
1 两者在内部都是调用了sendMessageDelayed方法实现的, handler.post和handler.sendMessage本质上是没有区别的,都是发送一个消息到消息队列中,而且消息队列和handler都是依赖于同一个线程的。
2 sendMessage接口的参数是message, 而post接口的参数是一个Runnable
(1) sendMessage/sendMessageDelayed
Message msg = new Message();
msg.what = "id";
msg.obj = obj;
mHandler.sendMessage(msg);
Handler handler = new Handler () {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_ID) {
swipeLayout.setRefreshing(false);
}
}
}
(2) post/postDelayed
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// do something....
}
}, 100);