-1

I upload files to a Network using the S3 Multipart api. The upload is done in a IntentService. Now the api supports resume support, and i want to allow a user to pause and resume the upload. Now the problem is when i allow the user to press a pause button, how can i kill the Intent Service? Fow this i would need to get the currently executing IntentService, and somehow call a function on it that saves the state of the uploading object and pause or kill it. Any idead how i can send a message to the currently executing intent service and kill it ?

Kind Regards

AndroidDev
  • 15,993
  • 29
  • 85
  • 119

1 Answers1

1

an IntentService stops itself when the onHandleIntent() method exits. that's one of the benefits of it over a regular service- you don't need to manage the life cycle.

from the Android Documentation page for IntentService.onHandleIntent(),

This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call stopSelf().

as far as pausing / resuming an upload, you'd need to be using non-blocking IO. there's no way to interrupt or otherwise stop blocking IO. the best you can do is set some state telling yourself to throw away the result when it's finished. non-blocking IO is a tad more complicated and beyond the scope what can be discussed here. here's one reasonable tutorial on the topic. if you are using a higher-level API (like the AWS Android API) to interact with S3, that API needs to support non-blocking IO.

in your case, you could keep state a database for each upload. when you get a result in your intent service, you can query the DB to see if the user canceled the upload.

if you are looking for a robust way to download files with pause / resume support, have you looked at the DownloadManager API?

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Can i send a message to the executing IntentService to prematurely exit the function ? – AndroidDev Aug 30 '12 at 15:17
  • see my comments about blocking vs. non-blocking IO. if you are using blocking IO, and your `IntentService` is blocking downloading a file, the answer is no. ultimately an `IntentService` is a separate thread, and just like any other thread, there's no magic "kill" method to stop it from executing. – Jeffrey Blattman Aug 30 '12 at 15:23
  • yes i saw that, but i have to upload and not downlaod. Thanks though. – AndroidDev Aug 30 '12 at 18:23
  • @JeffreyBlattman How do you know if the IntentService has finished? Is OnDestroy enough? What happens when the OS is low on memory and decides to kill some processes? Will the service restart itself when memory isn't low anymore? – android developer Jan 25 '15 at 23:02
  • @androiddeveloper it's not clear what you are trying to accomplish. with an `IntentService`, this idea is that you don't care about the lifecycle. it's something you don't need to worry about. if you need a service that hosts a long-running process, you should be using a regular service. – Jeffrey Blattman Jan 28 '15 at 17:41
  • @JeffreyBlattman Again, what happens if the service is killed due to low free RAM ? Will it get the intents that were on queue later? Also, is it possible to know when it's done? – android developer Jan 28 '15 at 20:05
  • anytime you start the service... it gets started. if it isn't in memory, it will be re-created. as is the case with any service, if you start it, it's guaranteed to run. don't worry about Android's memory management. Android defines a lifecycle for service, and intent services. that's all you need to know. – Jeffrey Blattman Jan 29 '15 at 16:34