1

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?

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

8

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().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm not sure I understand, but let me describe the exact nature of my concern. The InterntService is dealing with uploading files to the server. There's a client facing app where pictures are being taken, but the device is very likely in a place where there 3G connectivity is at 0. So the idea is to offload the images whenever possible. In my design the Alarm goes off every 15 minutes and calls the Intent which looks in the folder where the images are sitting and if it finds any, tries to upload them. However, it might take more than 15 minutes to upload them, and at that time the next – Yevgeny Simkin Feb 27 '11 at 01:34
  • Alarm will wake up the Intent and it might see the same files and try to upload them, even as the other one is already working on those files. Are you saying that the IntentService that the Alarm calls is going to be a solitary item on the execution stack and will simply keep going about its business without a second one being called into the mix? – Yevgeny Simkin Feb 27 '11 at 01:36
  • @Dr. Dredel: "it might see the same files and try to upload them, even as the other one is already working on those files" -- that is not possible unless your service is buggy. "Are you saying that the IntentService that the Alarm calls is going to be a solitary item...and will simply keep going about its business without a second one being called into the mix?" -- Yes. The 2nd alarm's `Intent` will be processed by the *same* `IntentService` *after* `onHandleIntent()` completes processing the 1st alarms' `Intent`. `IntentService` is a work queue, and all services are singletons. – CommonsWare Feb 27 '11 at 12:33
  • Really like this way: http://stackoverflow.com/questions/7440473/android-how-to-check-if-the-intent-service-is-still-running-or-has-stopped-runni – Renan Franca Aug 29 '13 at 05:01