Madhur Ahuja
Madhur Ahuja

Reputation: 22709

AsyncTask executed from Service

I have an AsyncTask executed from a service. Since AsyncTask runs in a separate thread, I have few doubts:

  1. What would happen if service onStartCommand() method returns while the AsyncTask is executed ? Does the service shut down while AsyncTask is executed ? How to best resolve this situation ?, should I use START_STICKY ?

  2. Since onPostExecute() runs on UIThread, Will it run on same thread as service's onStartCommand method ? What would happen in above case if AsyncTask is still running while service shuts down. Which thread would onPostExecute() upon ?

Update:

The reason I am using Service is because Service is instantiated from a onReceive method of a Broadcast Receiver. I would agree that I could just use an ordinary thread in onReceive but the service-AsyncTask pattern seems to be a better one because I need to fire some notifications as well which I am doing in onPreExecute and onPostExecute.

Upvotes: 1

Views: 666

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007659

I have an AsyncTask executed from a service

Since you rarely need to work on the main application thread in a Service, I would recommend just using an ordinary thread.

What would happen if service onStartCommand() method returns while the AsyncTask is executed ?

Nothing.

Does the service shut down while AsyncTask is executed ?

onStartCommand() is called when the service is sent a command. It is not called when the service is being shut down.

should I use START_STICKY ?

That is impossible to answer in the abstract. Use it if you need the service to be restarted later on (e.g., when the available memory improves).

Since onPostExecute() runs on UIThread, Will it run on same thread as service ?

Objects do not run on threads in Java. Hence, a service does not run on a thread. Methods run on threads.

What would happen in above case if AsyncTask is still running while service shuts down.

That is up to you, as you are the only one that shuts down the service.

If the OS gets rid of the service for other reasons, it will tend to do so by terminating the entire process, in which case the AsyncTask is gone too.

Which thread would onPostExeucte() upon ?

onPostExecute() runs on the main application thread. Since this is rarely needed, or even a good idea, in a Service, please consider using an ordinary thread rather than an AsyncTask.

Better yet, use an IntentService, if your work is transactional in nature (e.g., needs to run for several seconds, then the work is done and the service is no longer needed), as IntentService comes with its own background thread, so you do not need yours.

UPDATE

The reason I am using Service is because Service is instantiated from a onReceive method of a Broadcast Receiver

That has nothing to do with anything.

I need to fire some notifications as well which I am doing in onPreExecute and onPostExecute.

You can raise a Notification from a background thread. You do not need to do it from the main application thread.

Upvotes: 3

Related Questions