public class Binder extends Object implements IBinder
IBinder
.
This class is an implementation of IBinder that provides
the standard support creating a local implementation of such an object.
Most developers will not implement this class directly, instead using the aidl tool to describe the desired interface, having it generate the appropriate Binder subclass. You can, however, derive directly from Binder to implement your own custom RPC protocol or simply instantiate a raw Binder object directly to use as a token that can be shared across processes.
IBinder
IBinder.DeathRecipient
DUMP_TRANSACTION, FIRST_CALL_TRANSACTION, FLAG_ONEWAY, INTERFACE_TRANSACTION, LAST_CALL_TRANSACTION, LIKE_TRANSACTION, PING_TRANSACTION, SYSPROPS_TRANSACTION, TWEET_TRANSACTION
Constructor and Description |
---|
Binder()
Default constructor initializes the object.
|
Modifier and Type | Method and Description |
---|---|
void |
attachInterface(IInterface owner,
String descriptor)
Convenience method for associating a specific interface with the Binder.
|
static long |
clearCallingIdentity()
Reset the identity of the incoming IPC on the current thread.
|
protected void |
dump(FileDescriptor fd,
PrintWriter fout,
String[] args)
Print the object's state into the given stream.
|
void |
dump(FileDescriptor fd,
String[] args)
Implemented to call the more convenient version
dump(FileDescriptor, PrintWriter, String[]) . |
void |
dumpAsync(FileDescriptor fd,
String[] args)
Like
dump(FileDescriptor, String[]) , but ensures the target
executes asynchronously. |
protected void |
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
static void |
flushPendingCommands()
Flush any Binder commands pending in the current thread to the kernel
driver.
|
static int |
getCallingPid()
Return the ID of the process that sent you the current transaction
that is being processed.
|
static int |
getCallingUid()
Return the Linux uid assigned to the process that sent you the
current transaction that is being processed.
|
static UserHandle |
getCallingUserHandle()
Return the UserHandle assigned to the process that sent you the
current transaction that is being processed.
|
String |
getInterfaceDescriptor()
Default implementation returns an empty interface name.
|
static int |
getThreadStrictModePolicy()
Gets the current native thread-local StrictMode policy mask.
|
boolean |
isBinderAlive()
Check to see if the process that the binder is in is still alive.
|
static void |
joinThreadPool()
Add the calling thread to the IPC thread pool.
|
void |
linkToDeath(IBinder.DeathRecipient recipient,
int flags)
Local implementation is a no-op.
|
protected boolean |
onTransact(int code,
Parcel data,
Parcel reply,
int flags)
Default implementation is a stub that returns false.
|
boolean |
pingBinder()
Default implementation always returns true -- if you got here,
the object is alive.
|
IInterface |
queryLocalInterface(String descriptor)
Use information supplied to attachInterface() to return the
associated IInterface if it matches the requested
descriptor.
|
static void |
restoreCallingIdentity(long token)
Restore the identity of the incoming IPC on the current thread
back to a previously identity that was returned by
clearCallingIdentity() . |
static void |
setThreadStrictModePolicy(int policyMask)
Sets the native thread-local StrictMode policy mask.
|
boolean |
transact(int code,
Parcel data,
Parcel reply,
int flags)
Default implementation rewinds the parcels and calls onTransact.
|
boolean |
unlinkToDeath(IBinder.DeathRecipient recipient,
int flags)
Local implementation is a no-op.
|
public static final int getCallingPid()
public static final int getCallingUid()
public static final UserHandle getCallingUserHandle()
getCallingUid()
in that a
particular user will have multiple distinct apps running under it each
with their own uid. If the current thread is not currently executing an
incoming transaction, then its own UserHandle is returned.public static final long clearCallingIdentity()
restoreCallingIdentity(long)
.getCallingPid()
,
getCallingUid()
,
restoreCallingIdentity(long)
public static final void restoreCallingIdentity(long token)
clearCallingIdentity()
.token
- The opaque token that was previously returned by
clearCallingIdentity()
.clearCallingIdentity()
public static final void setThreadStrictModePolicy(int policyMask)
The StrictMode settings are kept in two places: a Java-level threadlocal for libcore/Dalvik, and a native threadlocal (set here) for propagation via Binder calls. This is a little unfortunate, but necessary to break otherwise more unfortunate dependencies either of Dalvik on Android, or Android native-only code on Dalvik.
StrictMode
public static final int getThreadStrictModePolicy()
setThreadStrictModePolicy(int)
public static final void flushPendingCommands()
public static final void joinThreadPool()
public void attachInterface(IInterface owner, String descriptor)
public String getInterfaceDescriptor()
getInterfaceDescriptor
in interface IBinder
public boolean pingBinder()
pingBinder
in interface IBinder
public boolean isBinderAlive()
isBinderAlive
in interface IBinder
public IInterface queryLocalInterface(String descriptor)
queryLocalInterface
in interface IBinder
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException
If you want to call this, call transact().
RemoteException
public void dump(FileDescriptor fd, String[] args)
dump(FileDescriptor, PrintWriter, String[])
.public void dumpAsync(FileDescriptor fd, String[] args)
dump(FileDescriptor, String[])
, but ensures the target
executes asynchronously.protected void dump(FileDescriptor fd, PrintWriter fout, String[] args)
fd
- The raw file descriptor that the dump is being sent to.fout
- The file to which you should dump your state. This will be
closed for you after you return.args
- additional arguments to the dump request.public final boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException
transact
in interface IBinder
code
- The action to perform. This should
be a number between IBinder.FIRST_CALL_TRANSACTION
and
IBinder.LAST_CALL_TRANSACTION
.data
- Marshalled data to send to the target. Must not be null.
If you are not sending any data, you must create an empty Parcel
that is given here.reply
- Marshalled data to be received from the target. May be
null if you are not interested in the return value.flags
- Additional operation flags. Either 0 for a normal
RPC, or IBinder.FLAG_ONEWAY
for a one-way RPC.RemoteException
public void linkToDeath(IBinder.DeathRecipient recipient, int flags)
linkToDeath
in interface IBinder
IBinder.unlinkToDeath(android.os.IBinder.DeathRecipient, int)
public boolean unlinkToDeath(IBinder.DeathRecipient recipient, int flags)
unlinkToDeath
in interface IBinder
DeathRecipient.binderDied()
method
will not be called. Returns false if the target IBinder has already
died, meaning the method has been (or soon will be) called.protected void finalize() throws Throwable
Object
Note that objects that override finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.