I have service that I need to run every 5 minutes. I already have code to schedule repeating Alarm on device boot. It works but
When installing app开发者_开发问答 and running it my alarm doesn't run. Need to reboot debice. This is expected.
If I start repeating alarm on activity start then I'm not sure if I already have alarm going from device boot. Will I get "doubles"? Will it run 2 alarms?
Basically, I want to make sure that I start alarm if user reboots device or battery dies, etc. But I also don't wan't them to reboot device when app installed to get it started. Or, for example if user "Force close" and then run app - it won't run this service anymore but it needs to. And, I don't want double-runs. I just need once every 5 minutes.
How this can/need to be done?
When installing app and running it my alarm doesn't run. Need to reboot debice. This is expected.
You do not have to wait until a reboot. Schedule your initial alarms on the first run of your app.
If I start repeating alarm on activity start then I'm not sure if I already have alarm going from device boot. Will I get "doubles"? Will it run 2 alarms?
So long as it is an equivalent PendingIntent
, it should replace the existing alarm, AFAIK.
If you or someone else is still having trouble with this, I can share what I ended up using to solve the problem.
First, let me restate the problem as I understand it, since there seems to have been some confusion: you would like to do something (in your case, set an alarm) once, when the application is first booted. Not when one of the application's activity is brought to the forefront. And not just once ever.
The first activity you load at boot has an onCreate method with a parameter that will help you greatly:
public void onCreate(Bundle savedInstanceState) {}
The first time this activity runs, savedInstanceState will equal null. Every time thereafter it will not be null. Use this to trigger whether or not to do something upon application boot. This is how I determine whether or not to start a service at boot.
精彩评论