Skip to content

Most visited

Recently visited

navigation

Binder

public class Binder
extends Object implements IBinder

java.lang.Object
   ↳ android.os.Binder


Base class for a remotable object, the core part of a lightweight remote procedure call mechanism defined by IBinder. This class is an implementation of IBinder that provides standard 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.

This class is just a basic IPC primitive; it has no impact on an application's lifecycle, and is valid only as long as the process that created it continues to run. To use this correctly, you must be doing so within the context of a top-level application component (a Service, Activity, or ContentProvider) that lets the system know your process should remain running.

You must keep in mind the situations in which your process could go away, and thus require that you later re-create a new Binder and re-attach it when the process starts again. For example, if you are using this within an Activity, your activity's process may be killed any time the activity is not started; if the activity is later re-created you will need to create a new Binder and hand it back to the correct place again; you need to be aware that your process may be started for another reason (for example to receive a broadcast) that will not involve re-creating the activity and thus run its code to create a new Binder.

See also:

Summary

Inherited constants

From interface android.os.IBinder

Public constructors

Binder()

Default constructor initializes the object.

Public methods

void attachInterface(IInterface owner, String descriptor)

Convenience method for associating a specific interface with the Binder.

static final long clearCallingIdentity()

Reset the identity of the incoming IPC on the current thread.

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.

static final void flushPendingCommands()

Flush any Binder commands pending in the current thread to the kernel driver.

static final int getCallingPid()

Return the ID of the process that sent you the current transaction that is being processed.

static final int getCallingUid()

Return the Linux uid assigned to the process that sent you the current transaction that is being processed.

static final 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.

boolean isBinderAlive()

Check to see if the process that the binder is in is still alive. Note that if you're calling on a local binder, this always returns true because your process is alive if you're calling it.

static final void joinThreadPool()

Add the calling thread to the IPC thread pool.

void linkToDeath(IBinder.DeathRecipient recipient, int flags)

Local implementation is a no-op.

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 final 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().

final 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.

Protected methods

void dump(FileDescriptor fd, PrintWriter fout, String[] args)

Print the object's state into the given stream.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

boolean onTransact(int code, Parcel data, Parcel reply, int flags)

Default implementation is a stub that returns false.

Inherited methods

From class java.lang.Object
From interface android.os.IBinder

Public constructors

Binder

Added in API level 1
Binder ()

Default constructor initializes the object.

Public methods

attachInterface

Added in API level 1
void attachInterface (IInterface owner, 
                String descriptor)

Convenience method for associating a specific interface with the Binder. After calling, queryLocalInterface() will be implemented for you to return the given owner IInterface when the corresponding descriptor is requested.

Parameters
owner IInterface
descriptor String

clearCallingIdentity

Added in API level 1
long clearCallingIdentity ()

Reset the identity of the incoming IPC on the current thread. This can be useful if, while handling an incoming call, you will be calling on interfaces of other objects that may be local to your process and need to do permission checks on the calls coming into them (so they will check the permission of your own local process, and not whatever process originally called you).

Returns
long Returns an opaque token that can be used to restore the original calling identity by passing it to restoreCallingIdentity(long).

See also:

dump

Added in API level 3
void dump (FileDescriptor fd, 
                String[] args)

Implemented to call the more convenient version dump(FileDescriptor, PrintWriter, String[]).

Parameters
fd FileDescriptor: The raw file descriptor that the dump is being sent to.
args String: additional arguments to the dump request.

dumpAsync

Added in API level 13
void dumpAsync (FileDescriptor fd, 
                String[] args)

Like dump(FileDescriptor, String[]), but ensures the target executes asynchronously.

Parameters
fd FileDescriptor: The raw file descriptor that the dump is being sent to.
args String: additional arguments to the dump request.

flushPendingCommands

Added in API level 1
void flushPendingCommands ()

Flush any Binder commands pending in the current thread to the kernel driver. This can be useful to call before performing an operation that may block for a long time, to ensure that any pending object references have been released in order to prevent the process from holding on to objects longer than it needs to.

getCallingPid

Added in API level 1
int getCallingPid ()

Return the ID of the process that sent you the current transaction that is being processed. This pid can be used with higher-level system services to determine its identity and check permissions. If the current thread is not currently executing an incoming transaction, then its own pid is returned.

Returns
int

getCallingUid

Added in API level 1
int getCallingUid ()

Return the Linux uid assigned to the process that sent you the current transaction that is being processed. This uid can be used with higher-level system services to determine its identity and check permissions. If the current thread is not currently executing an incoming transaction, then its own uid is returned.

Returns
int

getCallingUserHandle

Added in API level 17
UserHandle getCallingUserHandle ()

Return the UserHandle assigned to the process that sent you the current transaction that is being processed. This is the user of the caller. It is distinct from 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.

Returns
UserHandle

getInterfaceDescriptor

Added in API level 1
String getInterfaceDescriptor ()

Default implementation returns an empty interface name.

Returns
String

isBinderAlive

Added in API level 1
boolean isBinderAlive ()

Check to see if the process that the binder is in is still alive. Note that if you're calling on a local binder, this always returns true because your process is alive if you're calling it.

Returns
boolean false if the process is not alive. Note that if it returns true, the process may have died while the call is returning.

joinThreadPool

Added in API level 1
void joinThreadPool ()

Add the calling thread to the IPC thread pool. This function does not return until the current process is exiting.

linkToDeath

Added in API level 1
void linkToDeath (IBinder.DeathRecipient recipient, 
                int flags)

Local implementation is a no-op.

Parameters
recipient IBinder.DeathRecipient
flags int

pingBinder

Added in API level 1
boolean pingBinder ()

Default implementation always returns true -- if you got here, the object is alive.

Returns
boolean Returns false if the hosting process is gone, otherwise the result (always by default true) returned by the pingBinder() implementation on the other side.

queryLocalInterface

Added in API level 1
IInterface queryLocalInterface (String descriptor)

Use information supplied to attachInterface() to return the associated IInterface if it matches the requested descriptor.

Parameters
descriptor String
Returns
IInterface

restoreCallingIdentity

Added in API level 1
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().

Parameters
token long: The opaque token that was previously returned by clearCallingIdentity().

See also:

transact

Added in API level 1
boolean transact (int code, 
                Parcel data, 
                Parcel reply, 
                int flags)

Default implementation rewinds the parcels and calls onTransact. On the remote side, transact calls into the binder to do the IPC.

Parameters
code int: The action to perform. This should be a number between FIRST_CALL_TRANSACTION and LAST_CALL_TRANSACTION.
data Parcel: 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 Parcel: Marshalled data to be received from the target. May be null if you are not interested in the return value.
flags int: Additional operation flags. Either 0 for a normal RPC, or FLAG_ONEWAY for a one-way RPC.
Returns
boolean
Throws
RemoteException

unlinkToDeath

Added in API level 1
boolean unlinkToDeath (IBinder.DeathRecipient recipient, 
                int flags)

Local implementation is a no-op.

Parameters
recipient IBinder.DeathRecipient
flags int
Returns
boolean true if the recipient is successfully unlinked, assuring you that its DeathRecipient.binderDied() method will not be called; false if the target IBinder has already died, meaning the method has been (or soon will be) called.

Protected methods

dump

Added in API level 1
void dump (FileDescriptor fd, 
                PrintWriter fout, 
                String[] args)

Print the object's state into the given stream.

Parameters
fd FileDescriptor: The raw file descriptor that the dump is being sent to.
fout PrintWriter: The file to which you should dump your state. This will be closed for you after you return.
args String: additional arguments to the dump request.

finalize

Added in API level 1
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable

onTransact

Added in API level 1
boolean onTransact (int code, 
                Parcel data, 
                Parcel reply, 
                int flags)

Default implementation is a stub that returns false. You will want to override this to do the appropriate unmarshalling of transactions.

If you want to call this, call transact().

Parameters
code int
data Parcel
reply Parcel
flags int
Returns
boolean
Throws
RemoteException
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.