Binder.javaAPI DocAndroid 1.5 API12010Wed May 06 22:41:56 BST 2009android.os


public class Binder extends Object implements IBinder
Base class for a remotable object, the core part of a lightweight remote procedure call mechanism defined by {@link 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.


Fields Summary
private static final boolean
private static final String
private int
private IInterface
private String
Constructors Summary
public Binder()
Default constructor initializes the object.


            final Class<? extends Binder> klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Binder class should be static or leaks might occur: " +
Methods Summary
public voidattachInterface(IInterface owner, java.lang.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.

        mOwner = owner;
        mDescriptor = descriptor;
public static final native longclearCallingIdentity()
Reset the identity of the incoming IPC to the local process. 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 an opaque token that can be used to restore the original calling identity by passing it to {@link #restoreCallingIdentity(long)}.

private final native voiddestroy()

public voiddump( fd, java.lang.String[] args)
Implemented to call the more convenient version {@link #dump(FileDescriptor, PrintWriter, String[])}.

        FileOutputStream fout = new FileOutputStream(fd);
        PrintWriter pw = new PrintWriter(fout);
        try {
            dump(fd, pw, args);
        } finally {
protected voiddump( fd, fout, java.lang.String[] args)
Print the object's state into the given stream.

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.

private booleanexecTransact(int code, int dataObj, int replyObj, int flags)

        Parcel data = Parcel.obtain(dataObj);
        Parcel reply = Parcel.obtain(replyObj);
        // theoretically, we should call transact, which will call onTransact,
        // but all that does is rewind it, and we just got these from an IPC,
        // so we'll just call it directly.
        boolean res;
        try {
            res = onTransact(code, data, reply, flags);
        } catch (RemoteException e) {
            res = true;
        } catch (RuntimeException e) {
            res = true;
        return res;
protected voidfinalize()

        try {
        } finally {
public static final native voidflushPendingCommands()
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.

public static final native intgetCallingPid()
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.

public static final native intgetCallingUid()
Return the ID of the user 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.

public java.lang.StringgetInterfaceDescriptor()
Default implementation returns an empty interface name.

        return mDescriptor;
private final native voidinit()

public booleanisBinderAlive()
{@inheritDoc} Note that if you're calling on a local binder, this always returns true because your process is alive if you're calling it.

        return true;
public static final native voidjoinThreadPool()
Add the calling thread to the IPC thread pool. This function does not return until the current process is exiting.

public voidlinkToDeath(DeathRecipient recipient, int flags)
Local implementation is a no-op.

protected booleanonTransact(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().

        if (code == INTERFACE_TRANSACTION) {
            return true;
        } else if (code == DUMP_TRANSACTION) {
            ParcelFileDescriptor fd = data.readFileDescriptor();
            String[] args = data.readStringArray();
            if (fd != null) {
                try {
                    dump(fd.getFileDescriptor(), args);
                } finally {
                    try {
                    } catch (IOException e) {
            return true;
        return false;
public booleanpingBinder()
Default implementation always returns true -- if you got here, the object is alive.

        return true;
public IInterfacequeryLocalInterface(java.lang.String descriptor)
Use information supplied to attachInterface() to return the associated IInterface if it matches the requested descriptor.

        if (mDescriptor.equals(descriptor)) {
            return mOwner;
        return null;
public static final native voidrestoreCallingIdentity(long token)
Restore the identity of the incoming IPC back to a previously identity that was returned by {@link #clearCallingIdentity}.

token The opaque token that was previously returned by {@link #clearCallingIdentity}.

public final booleantransact(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.

        if (Config.LOGV) Log.v("Binder", "Transact: " + code + " to " + this);
        if (data != null) {
        boolean r = onTransact(code, data, reply, flags);
        if (reply != null) {
        return r;
public booleanunlinkToDeath(DeathRecipient recipient, int flags)
Local implementation is a no-op.

        return true;