|
Build 1.0_r1(from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectandroid.os.RemoteCallbackList<E>
public class RemoteCallbackList<E extends IInterface>
Takes care of the grunt work of maintaining a list of remote interfaces,
typically for the use of performing callbacks from a
Service to its clients. In particular, this:
IInterface callbacks,
taking care to identify them through their underlying unique IBinder
(by calling IInterface.asBinder().
IBinder.DeathRecipient to
each registered interface, so that it can be cleaned out of the list if its
process goes away.
To use this class, simply create a single instance along with your
service, and call its register(E) and unregister(E) methods
as client register and unregister with your service. To call back on to
the registered clients, use beginBroadcast(),
getBroadcastItem(int), and finishBroadcast().
If a registered callback's process goes away, this class will take
care of automatically removing it from the list. If you want to do
additional work in this situation, you can create a subclass that
implements the onCallbackDied(E) method.
| Field Summary | |
|---|---|
(package private) HashMap<IBinder,android.os.RemoteCallbackList.Callback> |
mCallbacks
|
| Constructor Summary | |
|---|---|
RemoteCallbackList()
|
|
| Method Summary | |
|---|---|
int |
beginBroadcast()
Prepare to start making calls to the currently registered callbacks. |
void |
finishBroadcast()
Clean up the state of a broadcast previously initiated by calling beginBroadcast(). |
E |
getBroadcastItem(int index)
Retrieve an item in the active broadcast that was previously started with beginBroadcast(). |
void |
kill()
Disable this callback list. |
void |
onCallbackDied(E callback)
Called when the process hosting a callback in the list has gone away. |
boolean |
register(E callback)
Add a new callback to the list. |
boolean |
unregister(E callback)
Remove from the list a callback that was previously added with register(E). |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
HashMap<IBinder,android.os.RemoteCallbackList.Callback> mCallbacks
| Constructor Detail |
|---|
public RemoteCallbackList()
| Method Detail |
|---|
public boolean register(E callback)
unregister(E) or its hosting process
goes away. If the callback was already registered (determined by
checking to see if the callback.asBinder()
object is already in the list), then it will be left as-is.
Registrations are not counted; a single call to unregister(E)
will remove a callback after any number calls to register it.
callback - The callback interface to be added to the list. Must
not be null -- passing null here will cause a NullPointerException.
Most services will want to check for null before calling this with
an object given from a client, so that clients can't crash the
service with bad data.
kill() had
previously been called or the callback's process has gone away.unregister(E),
kill(),
onCallbackDied(E)public boolean unregister(E callback)
register(E). This uses the
callback.asBinder() object to correctly
find the previous registration.
Registrations are not counted; a single unregister call will remove
a callback after any number calls to register(E) for it.
callback - The callback to be removed from the list. Passing
null here will cause a NullPointerException, so you will generally want
to check for null before calling.
register(E)public void kill()
register(E) will
fail. This should be used when a Service is stopping, to prevent clients
from registering callbacks after it is stopped.
register(E)public void onCallbackDied(E callback)
callback - The callback whose process has died. Note that, since
its process has died, you can not make any calls on to this interface.
You can, however, retrieve its IBinder and compare it with another
IBinder to see if it is the same object.register(E)public int beginBroadcast()
getBroadcastItem(int). Note that only one broadcast can
be active at a time, so you must be sure to always call this from the
same thread (usually by scheduling with Handler or
do your own synchronization. You must call finishBroadcast()
when done.
A typical loop delivering a broadcast looks like this:
final int N = callbacks.beginBroadcast(); for (int i=0; i
- Returns:
- Returns the number of callbacks in the broadcast, to be used with
getBroadcastItem(int)to determine the range of indices you can supply.- See Also:
getBroadcastItem(int),finishBroadcast()
public E getBroadcastItem(int index)
beginBroadcast(). This can only be called after
the broadcast is started, and its data is no longer valid after
calling finishBroadcast().
Note that it is possible for the process of one of the returned
callbacks to go away before you call it, so you will need to catch
RemoteException when calling on to the returned object.
The callback list itself, however, will take care of unregistering
these objects once it detects that it is no longer valid, so you can
handle such an exception by simply ignoring it.
index - Which of the registered callbacks you would like to
retrieve. Ranges from 0 to 1-beginBroadcast().
beginBroadcast()public void finishBroadcast()
beginBroadcast(). This must always be called when you are done
with a broadcast.
beginBroadcast()
|
Build 1.0_r1(from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||