当将值类型装箱为msg.obj

时间:2016-03-23 23:46:43

标签: android android-handler

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

给出以下代码:

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

问题是我发送邮件后10秒仍然会启动邮件!

为什么handler.removeMessages没有删除邮件?

1 个答案:

答案 0 :(得分:2)

问题是将long自动装箱到长对象。

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);

Pv的