Incoming calls on an IBinder interface are dispatched from a thread pool
maintained by the system in each process, so you basically can't make any
assumption about thread those methods will run on.  (This is basically the
"free threaded" model in COM if you are familiar with that.)

To put work on the main thread, simple have your main thread create a new
Handler() in its initialization, and from your interface methods you can
post messages to the handler.

Another approach you can take is instead of making your own aidl interface,
simple return a Messenger from your service that is pointing to a Handler
the service made.  Then interaction with the service is done by posting
messages to it through the Messenger, which will appear in the Handler
always running on the thread that created the Handler.  Note that with this
approach you can only perform async operations.

On Thu, Feb 12, 2009 at 6:16 AM, Hans <hkess...@gmail.com> wrote:

>
> Hi everyone,
>
> I'm an experienced C++/Java dev who has been truly enjoying that past
> two days of getting neck deep into the Android SDK.  I can foresee a
> lot of sleepless nights ahead :).
>
> Now, being arrogant due to experience (lol) I basically wrote a hello
> world Activity, got it to work and promptly decided to write an out of
> process service with accompanying client for project #2.
>
> Android is so well thought out (if not exactly documented although
> that's an 'over time' issue inmho) that I had basically implented
> everything but callbacks into the client before realizing (and feeling
> like a total idiot) that there's a very nice example called
> RemoteService (lol @ me - again...)
>
> Basically, this validated everything I'd been doing, although I did
> notice that because my client is in its own project and namespace and
> my service is in another project and namespace (but the same Eclipse
> workspace) that I had to have the service entry in my service's
> manifest AND my client's manifest (that one took me a while to figure
> out) in order to bind on the service.
>
> Anyhow, everything's great, I am starting to love on Android
> (figuratively) but I've run into <BRITISH-NESS>a bit of a sticky
> wicket, eh, wot?</BRITISH-NESS>.
>
> When my client Activity binds on my service (which is running in its
> own process) and calls the equivalent of 'registerCallback', the
> interface is added perfectly to the RemoteCallbackList object and I
> can immediately (right on the next line of code) use the interface to
> send a notification to the client.  Now, my problem is that when my
> main service thread tries to pull the interface out of the
> RemoteCallbackList via the broadcast methods, the RemoteCallbackList
> is always 'empty' - it returns 0 from beginBroadcast.
>
> After double checking ensure that I AM actually adding it to the list
> (because I've been a collossal idiot before) and that I am getting
> success back from 'register', I immediately think it's a threading
> issue, so:  I add logging code to the service in key places to log
> what the thread id is repeatedly, and I think "uh, I think I need to
> use a handler of some sort to make the call back in the thread that
> handled the registration" and to double check I call beginBroadcast
> right after 'register' and find that it always returns back the
> correct number of callback interfaces.  So it VERY much appears to be
> a threading issue (to me) so now I'm a bit stuck ->
>
> I've written a handler so that my main service thread can request the
> correct thread to actually call 'beginBroadcast' and then thought 'how
> is it going to know what thread that is...?' and instead am now
> thinking I need to pass, via a handler or something, the incoming
> interface from the thread that runs when the client calls the
> equivalent of 'registerCallback' to my main service thread.
>
> What part of the proper paradigm am I missing?
>
> BTW, as an example of the thread IDs, my main service thread is #1,
> the runnable I use for tasks in the main service thread shows an ID of
> #1, the message handler I was thinking I could use, shows an ID of #1,
> but the thread ID I get in my code in the service that runs when the
> user registers their callback is #7.
>
> If I plan to notify clients when the service notices something it
> thinks they need to know, do I need to notify (somehow) the clients
> via the thread with ID #7 or do I need to pass the interface I get
> when the user registers their callback from thread #7 to thread #1 and
> call 'register' on the RemoteCallbackList from thread #1?
>
> Sorry for the verbosity but I figured more is better than less.
> Again, everything works great except this one little part.
>
> :)
>
> >
>


-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to