I have a background Android service. It runs in background, checks an URL for some results. And if results change, app sends a notification to user. I want this service to run every 3 hours. My problem is when and how to start the service ?
- General behaviour is to create a Broadcast receiver.
- Then start service when receiver gets a
ACTION_BOOT_COMPLETED
. - Also when receiver gets a boot completed, receiver will use AlarmManager and will create an alarm to run every 3 hours.
But after installing the application, what if the user doesn't restart his/her phone ? If user doesn't boot, AlarmManager won't create an alarm.
- So another option is to create alarm when user first runs the application.
- What if user restarts the application ? Should I cancel all previous alarms with
AlarmManager.cancel
and recreate the alarms ?
So in which conditions do you set alarms for your Android applications ?