3

Recently, I'm working on an app that needs to run a Service every day at a certain time.

To do this, I use AlarmManager.

My question is: After activating the AlarmManager (from the Service), can I destroy the Service, and the AlarmManager will still call me in time?

If not, is there any way at a certain time to send me BroadcastRecerver, without running a Service all the time in OS?

Would appreciate help

Elyash
  • 59
  • 7

1 Answers1

2

The short answer to your question: service can still be started from the alarm manager anytime the alarm receiver is able to run. The long answer: usually it's good practice trying to use job schedulers though. They can both decide a better time to run, while ensuring you have the necessary resources to run successfully, such as network or high battery among other criteria.

Alarm managers are only reasonably reliable before Nougat. It's been long announced that developers should stop using it, and start using job schedulers for most use cases. They are meant to replace both the alarm setup and receiver, and allow the phone to save more battery by putting the phone in doze mode for longer and waking up and doing multiple tasks all at once.

Even if you get alarm manager working on your particular phone google makes less and less reliable with each OS release. There are very specific cases where alarms are still the way to go, but unless you're certain to be in one of these try to use job schedulers for all devices running lollipop and later. You can still use alarm manager reliably for kitkat and older, where job schedulers don't exist. More details on: https://developer.android.com/training/monitoring-device-state/doze-standby.html

In either case I think you need to listen for phone's boot so you can register your alarm/job scheduler: https://developer.android.com/training/scheduling/alarms.html

Job scheduler info: https://developer.android.com/topic/performance/scheduling.html

For reliability issues: Android AlarmManager not working on some devices when the app is closed


Fabio
  • 2,654
  • 16
  • 31