(注意:由于我的第一个版本不准确,我正在重新写我的问题)。

给出以下代码:

Handler handler = new Handler();
Message msg = handler.obtainMessage(12345, 67890L);
handler.sendMessageDelayed(message, 10000);
...
handler.removeMessages(12345, 67890L);


问题在于,我发送邮件后10秒钟仍会触发该邮件!

handler.removeMessages为什么不删除消息?

最佳答案

问题是将long自动装箱到Long Object。

MessageQueue void removeMessages(Handler h, int what, Object object)的调试器中:

638: // Remove all messages after front.
639: while (p != null) {
640:     Message n = p.next;
641:     if (n != null) {
642:         if (n.target == h && n.what == what
643:             && (object == null || n.obj == object)) {
644:             Message nn = n.next;
645:             n.recycleUnchecked();
646:             p.next = nn;
647:             continue;
648:         }
649:     }
650:     p = n;
651: }


在643行:

object = {java.lang.Long@4686} "67890"
n.obj = {java.lang.Long@4688} "67890"


因此,在我的情况下,比较总是失败,因为它是在比较实例,而盒装值实例始终是不同的。

尽管如此,我仍希望removeMessages的幕后实现如下:

// Remove all messages after front.
while (p != null) {
    Message n = p.next;
    if (n != null) {
        if (n.target == h && n.what == what
            && (object == null || object == n.obj || object.equals(n.obj))) {
            Message nn = n.next;
            n.recycleUnchecked();
            p.next = nn;
            continue;
        }
    }
    p = n;
}


我的解决方法是执行以下操作:

LongSparseArray<Object> objects = new LongSparseArray<>();
Handler handler = new Handler();

void sendMessageDelayed(int what, long key, long delayMillis)
{
    Object object = key;
    objects.put(key, object);
    Message msg = handler.obtainMessage(what, object);
    handler.sendMessageDelayed(msg, delayMillis);
}

void removeMessages(int what, long key)
{
    Object object = objects.get(key);
    if (object != null) {
        handler.removeMessages(what, object);
        objects.remove(key);
    }
}

sendMessageDelayed(12345, 67890L, 10000);
...
removeMessages(12345, 67890L);


光伏

08-05 20:45