有人可以解释 AlarmManeger 的这种奇怪行为吗?

发布于 2024-11-02 22:01:36 字数 2056 浏览 4 评论 0原文

以下是一个简单的警报调用程序,记下我要捕获的鳟鱼数量:

    private void setReminder() {
        Intent intent = new Intent(this, AlarmReceiver5.class);
        int trout = 21;
        intent.putExtra("intData", trout);
        intent.putExtra("textData", 
            "Great day for fishing! How many trout you want to get today? ");
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15 * 1000, sender);
    }
  1. 当我第一次运行它时,BroadcastReceiver 正确地看到我今天要捕获 21 条鳟鱼。

  2. 我在Eclipse中将数字更改为22,再次运行它,接收器仍然看到21!无论我在这里如何尝试,包括从Android卸载程序,接收器总是得到旧号码。

  3. 现在,如果我将接收器类从 AlarmReceiver5 重命名为 AlarmReceiver6,那么它可以成功获取不同的号码。但是同样的事情又发生了,即无论我如何更改号码,接收器总是获取旧号码,直到我重命名该类。

  4. 但是,如果我更改 Bundle 中的元素名称(即上例中的“intData”和“textData”),接收方将无法获取任何内容!数字变成0。当然我相应地更新了接收方。

  5. 即使我使用 ApiDemo 的 AlarmController 和 OneShotAlarm 类,上述情况也是如此。

现在,如果我稍微调整一下代码,我在设置下一个闹钟之前取消了闹钟,那么接收器不会收到任何信息,数字为 0。请参阅下面的代码:

    private void setReminder() {
        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver5.class);
        // Cancel the alarm
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        am.cancel(sender);

        // Then set it again (using a newly created PendingIntent).
        sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        int trout = 21;
        intent.putExtra("intData", trout);
        intent.putExtra("textData", 
        "Great day for fishing! How many trout you want to get today? ");
        sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        am.cancel(sender);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15 * 1000, sender);
}

有人可以解释一下 Android AlarmManager 的 hack 是怎么想的吗?谢谢你! (我可能要几个小时才能看到你的回复,因为我发布此消息后正在钓鱼。)

Following is a simple alarm invoking procedure, note the number of trout I want to catch:

    private void setReminder() {
        Intent intent = new Intent(this, AlarmReceiver5.class);
        int trout = 21;
        intent.putExtra("intData", trout);
        intent.putExtra("textData", 
            "Great day for fishing! How many trout you want to get today? ");
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15 * 1000, sender);
    }
  1. When I run it for the first time, the BroadcastReceiver correctly sees that I want to catch 21 trout today.

  2. I changed the number to 22 in Eclipse, run it again, the receiver still sees 21! No matter how I try here, including uninstalling the program from Android, the receiver always gets the old number.

  3. Now if I rename the receiver class from AlarmReceiver5 to AlarmReceiver6, then it can successfully get a different number. But then the same happened again, i.e., no matter how I change the number, the receiver always gets the old number until I rename the class.

  4. However, if I change the element names in the Bundle (i.e., "intData" and "textData" in the above example), the receiver can't get anything at all! The number becomes 0. Of course I updated the receiver side accordingly.

  5. The above is true even if I used ApiDemo's AlarmController and OneShotAlarm classes.

Now if I tweak the code a little more, I canceled the alarm before setting the next alarm, then the receiver doesn't get anything, the number is 0. See the code below:

    private void setReminder() {
        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver5.class);
        // Cancel the alarm
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        am.cancel(sender);

        // Then set it again (using a newly created PendingIntent).
        sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        int trout = 21;
        intent.putExtra("intData", trout);
        intent.putExtra("textData", 
        "Great day for fishing! How many trout you want to get today? ");
        sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        am.cancel(sender);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15 * 1000, sender);
}

Can somebody explain what the hack Android AlarmManager is thinking? Thank you! (I may not be able to see your reply for a few hours, because I'm out fishing after posting this.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2024-11-09 22:01:36

在 PendingIntent.getBroadcast(this, 0, 意图, 0);将标志参数(第四个)设置为 http:// 中的常量之一developer.android.com/reference/android/app/PendingIntent.html

in PendingIntent.getBroadcast(this, 0, intent, 0); set the flag parameter (fourth one) to one of the constants in http://developer.android.com/reference/android/app/PendingIntent.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文