Reputation: 709
I have a bound service. For example, the service has a method calculate() which does some really intensive calculation that should continue execution if activity is closed (that's why service is chosen, not the asynctask). And this service returns some result value which should be display on activity when it is opened again. Question is the following, how to gracefuly avoid ANR error in bound service if it has to return some value?
Upvotes: 2
Views: 1260
Reputation: 8978
If you're using Service
this doesn't mean you don't need a thread (or AsyncTask). You need actually.
An extract form the Service reference:
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work.
So, I'd just put the AsyncTask
inside the Service
in order to avoid ANRs.
update:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
You can update your main thread using:
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
Upvotes: 2