In an Android app, I need several listeners (for network changes, location changes, etc.).
As far as I can see, these are called in a thread (or threads) which are not my main background service thread. (I’m creating a service-only app.)
My question is:
Can my app be hit by listeners firing simultaneously, or does Android mitigate this by invoking my listeners in a single thread?
Secondly, if the listeners aren’t ‘sequentialised’, are the individual listeners re-entrant?
In other words, can I carry out complex processing in my listeners, or should they just dump what happened into a synchronized list, for later processing my background service thread (and wakeup the background service thread from its sleep while they’re at it).
There seems to be very little coverage of thread synchronisation in Android example docs and books.
Thanks,
Chris.
I don’t know what types of listeners you’re talking about specifically, but generally they are not called on their own threads. They are called on the thread that you register the listener on, so if you register the listener on the UI thread it will fire on the UI thread. If you create a
Looperthread, then you can register listeners on that thread, and they will fire in that thread sequentially as they hit.