简体   繁体   中英

How to create a service that runs in the background and performs async tasks in Android

I'm having great difficulty understanding when to use Service vs IntentService in Android.

I'm trying to create a Manager Class that can, download, verify and install APKs.

The process of doing this require me to spawn a service(DownloadManager) to download the file, which causes my service to be destroyed prematurely.

It also needs to run an activity to install the apk.

This download manager has no front end, I just want it to be a background process that does its thing and returns the results programmatically.

I've read into both Service and Intent Service and although the documentation clearly says that Intent Services are meant to be used when the processing should be done off the UI thread, but nearly every forum I visit says that you should not do async work inside an IntentService.

For example: Waiting for asynchronous callback in Android's IntentService

In general, an IntentService is useful for when you have discrete tasks that you want executed one at a time off of the UI thread. The IntentService will keep track of each request in a queue, execute one request at a time - on a separate, non-UI thread - and then will shut down when the queue is empty. If a new request arrives later, it will start up again, then shut down again once the queue is empty.

The warnings about running "async" work inside of an IntentService are because once onHandleIntent exits, the IntentService thinks that item has finished processing. It has no way of knowing if you created another thread that you want it to wait for. So once it has called onHandleIntent for all outstanding requests, it's going to shut down, even if there are child threads still running.

A non-intent Service gives you control over when the service starts and stops, regardless of whether there's any work to do. Also, unless you specifically make it otherwise, everything the Service does happens on the UI thread - so if you want work done on a background thread, you need to explicitly implement that. It's also up to you to implement how the Service handles multiple incoming requests. But, the service won't shut down until you tell it to (or the OS runs out of resources).

It sounds based on your description like you probably have two choices:

  1. If you're ok with the service processing requests one at a time, you could use an IntentService - but you'll need to make onHandleIntent wait for each request to finish. This is still happening off of the UI thread, but it does mean that if you have multiple download requests, they're not going to happen in parallel.

  2. You could use a non-intent Service to process each download request on its own child thread, all in parallel. Then it's up to you to keep track of all the processing threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM