Overwrite existing alarm

夙愿已清 提交于 2019-12-04 03:48:01

问题


I create an alaram in my app which is calling a BroadcastReceiver to setup notifications ever day with this code:

Intent intent = new Intent(Benachrichtigung.CUSTOM_INTENT);
PendingIntent pendingIntent  = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);

alram = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alram.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (24 * 60 * 60 * 1000), pendingIntent);

Now I want that the user can set the time for the norification so I have to call calendar.set with the new value. How can I overwrite the existing alarm with a new one?


回答1:


To cancel or update an alarm using AlarmManager the Intent must match using the filterEquals. So basically, you re-create the PendingIntent the same as you did for the original and AlarmManager will see that they are the same. This includes the REQUEST_CODE, INTENT_ACTION, and INTENT_DATA (I may be missing something there but those are important.

Note:

EXTRAS are not used in comparing the two Intents.

So if the two Intents are equal then the first will be overwritten. When I have more time I can try and find a resource to better explain that.

According to the Intent Docs filterEquals

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.



来源:https://stackoverflow.com/questions/17200069/overwrite-existing-alarm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!