1

I have an alarm clock app on the play store that works very well on most of the devices , but unfortunately some devices report the alarm does not fire on the time adjusted and i concluded from research that there are some devices that restrict apps running in the background and kills alarm manager !

I have handled doze mode using the following code :

if (Build.VERSION.SDK_INT >= 23)
{
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}
else if (Build.VERSION.SDK_INT >= 19)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}
else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}

However this seems not enough on some devices.

I have read that a foreground service can prevent the system from killing the app anyway , however i can't ensure this also since i don't have in hand the devices where the problem occurs.

I want to ensure my alarm works perfectly fine on all devices and handles all scenarios , so what are all possible things to do to make sure my app runs properly and is not killed by the system?

user2469133
  • 1,940
  • 3
  • 21
  • 33

1 Answers1

4

You should be using AlarmManagerCompat.setAlarmClock() to set a user visible alarm suitable for an alarm clock app. This API uses setAlarmClock() on API 21+, which as per its documentation:

these alarms will be allowed to trigger even if the system is in a low-power idle (a.k.a. doze) mode. The system may also do some prep-work when it sees that such an alarm coming up, to reduce the amount of background work that could happen if this causes the device to fully wake up -- this is to avoid situations such as a large number of devices having an alarm set at the same time in the morning, all waking up at that time and suddenly swamping the network with pending background work. As such, these types of alarms can be extremely expensive on battery use and should only be used for their intended purpose.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thank you , do think this method will also prevent the system from force killing the app and thus the alarm manager ? I saw two big apps in the store still recommending to manually white list their app in "do not disturb" mode list in the phone settings @ianhanniballake – user2469133 Apr 07 '19 at 23:32
  • As long as you're using [`CATEGORY_ALARM`](https://developer.android.com/reference/androidx/core/app/NotificationCompat#CATEGORY_ALARM), you'll not have to worry about "Do Not Disturb". That's totally separate for force stopping though - that'll always cancel *everything* your app can do to wake up, alarms included. – ianhanniballake Apr 07 '19 at 23:43
  • I just understood lately the "dnd" is concerned with sounds only not battery optimization , my alarm app is exceptional since it uses phone media sound not phone alarm sound , so i think i would not be worried about "dnd" anyway. Another question , is setAlarmClock() exact ? In the broadcast receiver i check for the minutes and hours of the cached alarm and compare with current time , so if the hours and minutes were not exactly the same , then i cancel my alarm. @ianhanniballake – user2469133 Apr 07 '19 at 23:59
  • Yes, `setAlarmClock()` is exact. I don't know why you'd complicate your `BroadcastReceiver` vs just calling `cancel()` on `AlarmManager` and saving the whole device from waking up. – ianhanniballake Apr 08 '19 at 00:19
  • this check i do because when you change the devices clock , the broadcast receiver is called automatically even if it was in the wrong time . Same thing happens when you boot the device , broadcast receiver may fire in the wrong time . Do you know a work around for this issue , rather than checking time inside the broadcast receiver ? – user2469133 Apr 08 '19 at 11:01