Editing scheduled pending intends

前端 未结 1 724
栀梦
栀梦 2021-01-24 18:01

I wrote an app to turn on/off WiFi at scheduled time choosen before. The way it works is preety simple: Choose time from timepicker, then just add it. Programmatically it gets d

1条回答
  •  走了就别回头了
    2021-01-24 18:23

    When you set an alarm, you pass a PendingIntent to AlarmManager. The PendingIntent wraps an Intent. When you set an alarm, AlarmManager deletes any alarms it already has scheduled that match the PendingIntent. To determine if the PendingIntents match, the following are compared:

    • The requestCode in the PendingIntent.getBroadcast() call
    • The ACTION in the Intent
    • The CATEGORIES in the Intent
    • The DATA in the Intent
    • The Component (package name, class name) in the Intent

    NOTE: "extras" in the Intent are not compared

    So, if we want to reschedule an alarm, you just need to create a PendingIntent with the same requestCode, ACTION and Component as the previous one and set the alarm again. AlarmManager will remove the previous one and set the new one.

    If you want to have multiple alarms scheduled in parallel, you need to ensure that either the requestCodes, the Components or the ACTIONs are different, otherwise they will overwrite each other.

    0 讨论(0)
提交回复
热议问题