views:

66

answers:

1

I have been watching the Google I/O presentation by Virgil Dobjanschi on the correct way to implement REST on Android with services and content providers. http://code.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

Based on the video I'm doing things the wrong way, which is not surprising given how hard the right way is. Anyway, having been shown the promised land in the video I'm having a bit of a problem figuring out how to get there.

I have most of it nailed but the one thing that's defeating me is the Binder Callback that Virgil references in the slides (see link above), on page 43 it shows step 2 as starting the service with startService( intent ) and step 10 as returning status information using a Binder Callback. There is no example code anywhere in the presentation which is rather frustrating. There is talk of open sourcing the Twitter client which apparently uses this approach but nothing yet and the announcement was in April.

In the video he states: "What is a binder callback? A binder callback, think of it as an interface that was passed in the request intent."

I have searched all over the place but have not been able to find any doc or examples that show how to pass a callback as part of an intent. Nor can I figure out any other way of passing in a callback.

I thought he may be referring to binding to the service and implementing the callback that way. However, he is specifically referring to a local service and using strarSerice() and not bindService(). Also with bindService() the service will be destroyed if the activity is destroyed which defeats the idea. The workaround is to use startService() and then bindService() and leave the service running for the duration. The other problem with bindService() is that the callback can not be use until onServiceConnected() completes which complicates the code even further as the action instructions can not be passed in the intent because the return callback may not be in place in time to return the results.

I'm only interested in implementing this using the recommended approach. Can anyone shed an light on what a Binder Callback is and how to code it up. Also related to this, does anyone know what a Service Helper would look like apart from binge a singleton?

Any help here would be much appreciated. Thanks Clive

+1  A: 

He might have been referring to a ResultReceiver, or possibly createPendingResult(). Here is a pair of sample projects demonstrating the use of the latter.

CommonsWare
Great, thank you, I was not familiar with ResultReceiver but it certainly seems to fit what Virgil was talking about, I have implemented it and it's working fine.Just curious, why did you switch from ResultReceiver to createPendingResult()?
Clive Jackson
@Clive Jackson: Struggled a while with `ResultReceiver` and elected to put it aside for the time being. I'll probably come back to it someday.
CommonsWare