0

I have two Broadcast receivers which are scheduled to run according to:

mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 1000, 20000, shortNotification);

mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 30000, 30000, breakNotification);

where shortNotification and breakNotification:

breakNotification = PendingIntent.getBroadcast(this, 0, intentBreak, 0);
shortNotification = PendingIntent.getBroadcast(this, 0, intentShort, 0);

and intentBreak, intentShort are:

intentShort = new Intent(this, ShortBreakBroadcast.class);
intentBreak = new Intent(this, LongBreakBroadcast.class);

Using another answer (https://stackoverflow.com/a/7439072/4132650) I have tried to stop the two broadcast receivers in onDestroy when I close the app (by swiping left, not by pressing the home button) but it didn't work:

TimerViewActivity.this.getPackageManager().setComponentEnabledSetting(shortBreakComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
TimerViewActivity.this.getPackageManager().setComponentEnabledSetting(longBreakComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

How can I make them stop?

My app has 2 alarms. 1 is meant as a short break reminder while the 2nd is meant to remind the user to take a 10 min break (these times are shortened for testing). Both alarms start a broadcast receiver: the first one starts a short break receiver and the 10 min break one starts a second different receiver. I track whether the user is taking a 10 min break using an accelerometer. Both receivers send a notification and start playing music for 10 seconds.

When I close the app, I want all the processes to stop running: notifications, music, both broadcast receivers, and the accelerometer. That doesn't happen because apparently the two receivers are still running and initiating the above processes.

Here is the Github link: https://github.com/felixglusch/AccelTimer

Community
  • 1
  • 1
  • 2
    AlarmManager.cancel(PendingIntent) will kill the alarm. But since onDestroy isn't always called you can't be sure of when to call this. A 100% solution requires you to detect when you shouldn't call it in the receiver. – Gabe Sechan Apr 18 '16 at 00:29
  • @GabeSechan logcat says onDestroy is called every time I close the app. – Felix Gluschenkov Apr 18 '16 at 01:00
  • It normally will. Just don't count on it always. One case it never will is when it crashes, for example. – Gabe Sechan Apr 18 '16 at 01:02
  • Thanks! But I don't think I fully understand what you meant when you suggested to detect when I shouldn't call alarmmanager.cancel in the receiver. Do you mean if onDestroy wasn't called then don't cancel the alarms? – Felix Gluschenkov Apr 18 '16 at 01:25
  • How would I do that in the receiver? – Felix Gluschenkov Apr 18 '16 at 01:46

0 Answers0