Reputation: 28349
I have an AlarmService that wakes up every 15 minutes and fires off an IntentService. However, I would like to make sure that if a previously started IS is already running, that the AlarmService doesn't try to start another one (the IS is dealing with files and there would be an odd race condition if a second version tried to act on the same files).
What's the best way to poll the system to see if an instance of my IS is already running and just skip the current iteration of the AlarmService cron?
Upvotes: 1
Views: 2011
Reputation: 1006584
that the AlarmService doesn't try to start another one (the IS is dealing with files and there would be an odd race condition if a second version tried to act on the same files).
There will be no race condition. First, there will only ever be one instance of the IntentService
. Second, the IntentService
will only process one Intent
at a time in onHandleIntent()
.
What's the best way to poll the system to see if an instance of my IS is already running and just skip the current iteration of the AlarmService cron?
Don't do it that way. In your IntentService
, track the last time you did work in a data member. If, in onHandleIntent()
, that value is null
, it's your first pass through in a while, and so you probably want to go ahead and do the work. If the value is not null
, compare it to the current time, and if it's too soon, just return from onHandleIntent()
.
Upvotes: 8