Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Cancel the Pending Intent #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
boolean alarmUp = (PendingIntent.getBroadcast(this, 0, notifyIntent,
PendingIntent.FLAG_NO_CREATE) != null);

//only depends on the Pending Intent so remember to manage the lifecycle of the Pending Intent properly
alarmToggle.setChecked(alarmUp);

//Set up the PendingIntent for the AlarmManager
Expand Down Expand Up @@ -79,6 +80,10 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//Cancel the alarm and notification if the alarm is turned off
alarmManager.cancel(notifyPendingIntent);
mNotificationManager.cancelAll();

//Cancel the Pending Intent when you cancel the alarm.
//Not doing this can cause the toggle button to show up in the "ON" position even when the alarm is cancelled.
notifyPendingIntent.cancel();

//Set the toast message for the "off" case
toastMessage = getString(R.string.alarm_off_toast);
Expand Down