views:

1609

answers:

2

Here's scenario:

  1. Client makes remote call to the service (returns void) and provides a callback object
  2. Service executes some long running logic on the background thread and then uses callback object to trigger ether success or failure which (since these manipulate visual elements) execute in Activity#runOnUiThread block

The scenario runs fine. The question is - can I use AsyncTask to make code less verbose (how?) and would be there any advantages in doing it that way?

Or should I just get away from client callbacks alltogether and execute remote service calls retrofitted to return some value within AsyncTask#doInBackground?

+1  A: 

It is difficult to say whether AsyncTask will make things less verbose, since we don't know the verbosity of your current implementation.

For me, AsyncTask means I don't have to worry about cleaning up threads myself (e.g., post some sort of kill job to a LinkedBlockingQueue my background thread is waiting on). It also eliminates the custom Job classes I used to create for using with LinkedBlockingQueues. And, it simplifies a bit doing final work back on the UI thread.

In your case, with a remote service, the UI thread issue is less critical, since the activity needs to handle that itself.

I don't see what the difference is between your #2 and your last paragraph. In both cases, your service will call the callback object, which will use something like runOnUiThread() to arrange for the work to be done on the UI thread.

AFAIK, the only two ways to have a service doing any sort of asynchronous work let the client know that work is done is by a broadcast Intent or a callback object. Broadcast Intents are convenient but public (i.e., other code can watch for them).

I suspect I probably have not helped much here, but I just don't know enough of your scenario to provide greater detail.

CommonsWare
The scenario is not very original. I have a service that queries some XML from the remote location. When activity asks for XML by providing URL then it ether gets cached copy right back or gets in the waiting mode and waits for service to complete/fail.I was happily coding it with callbacks, basic threads and runOnUiThread when I noticed your usage of AsyncTask in EndlessAdapter. Which got me curious if I'm missing some simpler way of doing what I'm doing.BTW - thanks for your answer, it's great as usual!
DroidIn.net
A: 

I'm having quite the same question : i'm developping a map activity, with a 'lazy-loading' functionnality (xml from Network, parsing it, then updating my map with the 'items' created from that parsing...)

i wondered what would be 'the best' way to implement it...

  • async service launched from a thread, an update notification via Intent?
  • just a thread (no service, since i don't need to expose it to other applications) w/ callback
  • asyncTask with callback i'm comparingthese in terms of speed, using the Android SDK performance analysis Tool traceview

I guess a more precise answer might be found from Android contributors on the Android-developper-group...

VinzzzBarto
I think Mark lately always suggests using AlertManager to do scheduled updates rather than services.
DroidIn.net