With the help of some folks on the Beginner's discussion group, I got it figured out.
Basically, the best way to send/receive data between a local service and an activity is to use the transact()
method. You can call it on the IBinder
object, which is given to you in the onServiceConnected()
method of your ServiceConnection
object (confused yet?). The parameters to pass to it are mentioned in the docs.
Note, however, that bindService()
is asynchronous - it gets called 'sometime', and execution of your code (including transact()
, if it's after a bindService()
call) continues whether or not the service has been bound yet. So the best way to do transact()
calls that I've found is to put them in a separate (i.e., serviceTransaction()
) method, which is called from the onServiceConnected
method of the ServiceConnection object - that way you don't get a NullPointerException
when you try to call transact() on an IBinder
that doesn't exist, because the service hasn't yet been bound.
All that's well and good, but there's one more critical piece to this:
To get a service to properly return something (via the reply Parcel
you gave it in the original transact()
call), you need to go into the Service
's class and fill out the onTransact()
method in your overridden IBinder
object (in the demos it's something like mBinder), so that it updates that reply parcel with results before returning it.
I will try to post some sample code for folks within the next few days, but it's going to be hectic - I'll get it up as soon as I can.
If you're looking for getting data to/from a remote service, this tutorial
goes very in-depth on the subject. Good luck!