Issue with cancelling the AlarmManager - PendingIntent

后端 未结 5 1831
轻奢々
轻奢々 2020-12-19 01:49

I have an app which reminds people to do their tasks. So there is one PendingIntent, now the user can delete the alarm when he wants to. In this code, there is just one Pend

5条回答
  •  借酒劲吻你
    2020-12-19 02:53

    As it's stated in android documentation pending intents with intent that are equivalent as per Intent.filterEquals but have different request code are considered different:

    If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

    So, you can assign different request code and cancel the pending intents base on them and forget about the extra.

    There was an interesting scenario that I figured out this behavior:

    I scheduled an alarm in my code and run it on the device but never canceled it. Then I changed the request code and run it again. So a new alarm was created. I canceled the new alarm but the alarm was still executing from previous code. I get confused why the alarm is not canceled. After I found out it's from the previous code with different request code I uninstalled the app and installed it again and problem was solved.

提交回复
热议问题