Android AlarmManager: is there a way to cancell ALL the alarms set?

后端 未结 3 2109
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 18:58

I am building an app that set 2 alarms for each day of the week (at a certain hour and minute), the alarms repeat week after week forever.

Now the point is: if the u

3条回答
  •  清歌不尽
    2021-01-01 19:16

    I had same problem for cancelling alarms, and solved it. What you should do is simply do -

    1. When you create alarms, save request code of PendingIntent object (to shared preference).
    2. At later, when you want to cancel the alarm, create the same PendingIntent with the same request code (obviously from shared preference).
    3. Call cancel() of AlarmManager and pass the PendingIntent object in it, and alarm will be cancelled.

      private void cancelAlarm(int requestCode) {
      
      AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      
      PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(),requestCode,new Intent(this, MyBroadCastReceiver.class),0);
      alarmManager2.cancel(pendingIntent2);
      
      Toast.makeText(getApplicationContext(), "Alarm Cancelled - "+ requestCode, Toast.LENGTH_LONG).show();
      

      }

提交回复
热议问题