Android Asynchronous Calling Strategy

Here's the script:

  • Client makes a remote call to the service (returns void) and provides a callback object
  • The service executes some long logic on a background thread and then uses a callback object to trigger the success or failure of the ether which (since these visual manipulations) are done in the Activity # runOnUiThread block

The script works fine. The question is, can I use AsyncTask to generate less verbose code (how?), And would it have some advantage in doing it this way?

Or do I just need to get away from alltogether client callbacks and do remote service calls modified to return some value within AsyncTask # doInBackground?

+2


source to share


2 answers


It's hard to say if AsyncTask

anything less verbose will do, since we don't know the verbosity of your current implementation.

AsyncTask

Means to me that I don't have to worry about cleaning up the threads themselves (e.g. sending some kind of kill job to LinkedBlockingQueue

my background thread waiting). It also excludes the custom classes Job

I used to create with LinkedBlockingQueues

. And it makes it easier to do the final work on the UI thread.

In your case with a remote service, the UI thread issue is less critical, as you need to handle this operation yourself.



I don't see what the difference is between your # 2 and your last paragraph. In both cases, your service will invoke a callback object that will use something like this runOnUiThread()

to organize the work on the UI thread.

AFAIK, the only two ways to have a service doing any asynchronous work is to let the client know that the work is being done via a broadcast Intent

or callback object. Broadcasting is Intents

handy, but publicly available (i.e. other code might be watching them). In the meantime, there is no need to know about it. ”

I suspect it probably didn't work for me here, but I just don't know enough of your script to provide more details.

+3


source


I am having the same question: I am developing a map activity using the "lazy-load" functionnality (xml from Network, parses it and then updates my map with the "items" generated from this parsing ...)

I wondered what would be the "best" way to implement it ...



  • asynchronous service started from thread, update notification via intent?
  • just a stream (no service, since I don't need to expose it to other apps) w / callback
  • asyncTask with callback I compare this in terms of speed using Android SDK performance analysis tool traceview

I think a more accurate answer can be found from Android Developers in the Android Development Group ...

0


source







All Articles