Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a binder callback on Android?

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

like image 965
Clive Jackson Avatar asked Aug 31 '10 11:08

Clive Jackson


People also ask

What is the purpose of binder in Android?

Binders are the cornerstone of Android's architecture. It abstracts the low-level details of IPC from the developer, allowing applications to easily talk to both the System Server and others' remote service components.

What is a binder transaction?

Binder is an Android-specific interprocess communication mechanism, and remote method invocation system. That is, one Android process can call a routine in another Android process, using binder to indentify the method to invoke and pass the arguments between processes.

What are binder threads in Android?

Those threads are called Binder Threads. This grants the Service the ability to work on multiple calls happening at the same time. Usually, Service calls with the interface defined by "Extending the Binder class" and "Using a Messenger" are executed sequentially in one thread.

What is Java binder?

android.os.Binder. Base class for a remotable object, the core part of a lightweight remote procedure call mechanism defined by IBinder . This class is an implementation of IBinder that provides standard local implementation of such an object.


1 Answers

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

like image 139
CommonsWare Avatar answered Sep 27 '22 20:09

CommonsWare