最近遇到一个使用Handler removeMessages时使 使用boolean postDelayed(Runnable r, long delayMillis)函数post的Runnable也被删除而没有执行的问题,具体如下:
public class MainActivity extends Activity {
static final String TAG_STRING = "Main";
enum MSG_ID {
ID_FIRST,
ID_SECOND,
ID_THRED
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.e(TAG_STRING, " handleMessage what = " + msg.what);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG_STRING, "onCreate");
mHandler.sendEmptyMessageDelayed(MSG_ID.ID_FIRST.ordinal(), 1000);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.e(TAG_STRING, "run");
}
}, 1000);
mHandler.removeMessages(MSG_ID.ID_FIRST.ordinal());
}
}
public final boolean postDelayed(Runnable r, long delayMillis)
{
return sendMessageDelayed(getPostMessage(r), delayMillis);
}
getPostMessage的源码如下:
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
Message.obtain()的代码如下:
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
sPoolSize--;
return m;
}
}
return new Message();
}
以上代码最终会执行到 return new Message();
Message的构造函数为空
public Message() {
}
那么Message的what成员被默认赋值为0.
mHandler.removeMessages(MSG_ID.ID_FIRST.ordinal());
使用postDelayed post出去的Runnable, 如果还没被执行, 也会被删除掉。与此相同的还有
public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
{
return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
}
enum MSG_ID {
ID_NOT_USE,
ID_FIRST,
ID_SECOND,
ID_THRED
}