有人可以解释 AlarmManeger 的这种奇怪行为吗?
以下是一个简单的警报调用程序,记下我要捕获的鳟鱼数量:
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);
}
当我第一次运行它时,BroadcastReceiver 正确地看到我今天要捕获 21 条鳟鱼。
我在Eclipse中将数字更改为22,再次运行它,接收器仍然看到21!无论我在这里如何尝试,包括从Android卸载程序,接收器总是得到旧号码。
现在,如果我将接收器类从 AlarmReceiver5 重命名为 AlarmReceiver6,那么它可以成功获取不同的号码。但是同样的事情又发生了,即无论我如何更改号码,接收器总是获取旧号码,直到我重命名该类。
但是,如果我更改 Bundle 中的元素名称(即上例中的“intData”和“textData”),接收方将无法获取任何内容!数字变成0。当然我相应地更新了接收方。
即使我使用 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);
}
When I run it for the first time, the BroadcastReceiver correctly sees that I want to catch 21 trout today.
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.
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.
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 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