Added in API level 1

Binder


open class Binder : IBinder
kotlin.Any
   ↳ 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 android.app.Service, android.app.Activity, or android.content.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 android.app.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.

Summary

Inherited constants
Public constructors

Default constructor just initializes the object.

Binder(descriptor: String?)

Constructor for creating a raw Binder object (token) along with a descriptor.

Public methods
open Unit
attachInterface(owner: IInterface?, descriptor: String?)

Convenience method for associating a specific interface with the Binder.

static Long

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

static Long

Clears the work source on this thread.

open Unit
dump(fd: FileDescriptor, args: Array<String!>?)

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

open Unit

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

static Unit

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

static Int

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

static Int

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

static Int

Return the Linux UID assigned to the process that sent the transaction currently being processed.

static UserHandle

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

static Int

Returns the work source set by the caller.

open String?

Default implementation returns an empty interface name.

open Boolean

Check to see if the process that the binder is in is still alive.

static Unit

Add the calling thread to the IPC thread pool.

open Unit
linkToDeath(recipient: IBinder.DeathRecipient, flags: Int)

Local implementation is a no-op.

open Boolean

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

open IInterface?

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

static Unit

Restore the identity of the incoming IPC on the current thread back to a previously identity that was returned by clearCallingIdentity.

static Unit

Restores the work source on this thread using a token returned by setCallingWorkSourceUid(int) or clearCallingWorkSource().

static Long

Sets the work source for this thread.

Boolean
transact(code: Int, data: Parcel, reply: Parcel?, flags: Int)

Default implementation rewinds the parcels and calls onTransact.

open Boolean

Local implementation is a no-op.

Protected methods
open Unit
dump(fd: FileDescriptor, fout: PrintWriter, args: Array<String!>?)

Print the object's state into the given stream.

open Boolean
onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int)

Default implementation is a stub that returns false.

Public constructors

Binder

Added in API level 1
Binder()

Default constructor just initializes the object.

If you're creating a Binder token (a Binder object without an attached interface), you should use Binder(java.lang.String) instead.

Binder

Added in API level 29
Binder(descriptor: String?)

Constructor for creating a raw Binder object (token) along with a descriptor.

The descriptor of binder objects usually specifies the interface they are implementing. In case of binder tokens, no interface is implemented, and the descriptor can be used as a sort of tag to help identify the binder token. This will help identify remote references to these objects more easily when debugging.

Parameters
descriptor String?: Used to identify the creator of this token, for example the class name. Instead of creating multiple tokens with the same descriptor, consider adding a suffix to help identify them. This value may be null.

Public methods

attachInterface

Added in API level 1
open fun attachInterface(
    owner: IInterface?,
    descriptor: String?
): Unit

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?: This value may be null.
descriptor String?: This value may be null.

clearCallingIdentity

Added in API level 1
static fun clearCallingIdentity(): Long

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

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

clearCallingWorkSource

Added in API level 29
static fun clearCallingWorkSource(): Long

Clears the work source on this thread.

The work source will be propagated for future outgoing binder transactions executed on this thread.

restoreCallingWorkSource(long) must always be called after clearing the worksource.

A typical use case would be

long token = Binder.clearCallingWorkSource();
  try {
    // Call an API.
  } finally {
    Binder.restoreCallingWorkSource(token);
  }
  
Return
Long token to restore original work source.

dump

Added in API level 3
open fun dump(
    fd: FileDescriptor,
    args: Array<String!>?
): Unit

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

Parameters
fd FileDescriptor: This value cannot be null.
args Array<String!>?: This value may be null.

dumpAsync

Added in API level 13
open fun dumpAsync(
    fd: FileDescriptor,
    args: Array<String!>?
): Unit

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

Parameters
fd FileDescriptor: This value cannot be null.
args Array<String!>?: This value may be null.

flushPendingCommands

Added in API level 1
static fun flushPendingCommands(): Unit

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
static fun getCallingPid(): Int

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. Warning: oneway transactions do not receive PID. Even if you expect a transaction to be synchronous, a misbehaving client could send it as a asynchronous call and result in a 0 PID here. Additionally, if there is a race and the calling process dies, the PID may still be 0 for a synchronous call.

getCallingUid

Added in API level 1
static fun getCallingUid(): Int

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.

getCallingUidOrThrow

Added in API level 29
static fun getCallingUidOrThrow(): Int

Return the Linux UID assigned to the process that sent the transaction currently being processed.

Exceptions
java.lang.IllegalStateException if the current thread is not currently executing an incoming transaction and the calling identity has not been explicitly set with clearCallingIdentity()

getCallingUserHandle

Added in API level 17
static fun getCallingUserHandle(): UserHandle

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.

Return
UserHandle This value cannot be null.

getCallingWorkSourceUid

Added in API level 29
static fun getCallingWorkSourceUid(): Int

Returns the work source set by the caller.

Unlike getCallingUid(), this result of this method cannot be trusted. The caller can set the value to whatever they want. Only use this value if you trust the calling UID.

Return
Int The original UID responsible for the binder transaction.

getInterfaceDescriptor

Added in API level 1
open fun getInterfaceDescriptor(): String?

Default implementation returns an empty interface name.

Return
String? This value may be null.

isBinderAlive

Added in API level 1
open fun isBinderAlive(): Boolean

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.

Return
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
static fun joinThreadPool(): Unit

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
open fun linkToDeath(
    recipient: IBinder.DeathRecipient,
    flags: Int
): Unit

Local implementation is a no-op.

Parameters
recipient IBinder.DeathRecipient: This value cannot be null.
Exceptions
android.os.RemoteException if the target IBinder's process has already died.

pingBinder

Added in API level 1
open fun pingBinder(): Boolean

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

Return
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
open fun queryLocalInterface(descriptor: String): IInterface?

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

Parameters
descriptor String: This value cannot be null.
Return
IInterface? This value may be null.

restoreCallingIdentity

Added in API level 1
static fun restoreCallingIdentity(token: Long): Unit

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.

restoreCallingWorkSource

Added in API level 29
static fun restoreCallingWorkSource(token: Long): Unit

Restores the work source on this thread using a token returned by setCallingWorkSourceUid(int) or clearCallingWorkSource().

A typical use case would be

long token = Binder.setCallingWorkSourceUid(uid);
  try {
    // Call an API.
  } finally {
    Binder.restoreCallingWorkSource(token);
  }
  

setCallingWorkSourceUid

Added in API level 29
static fun setCallingWorkSourceUid(workSource: Int): Long

Sets the work source for this thread.

All the following binder calls on this thread will use the provided work source. If this is called during an on-going binder transaction, all the following binder calls will use the work source until the end of the transaction.

The concept of worksource is similar to WorkSource. However, for performance reasons, we only support one UID. This UID represents the original user responsible for the binder calls.

restoreCallingWorkSource(long) must always be called after setting the worksource.

A typical use case would be

long token = Binder.setCallingWorkSourceUid(uid);
  try {
    // Call an API.
  } finally {
    Binder.restoreCallingWorkSource(token);
  }
  

The work source will be propagated for future outgoing binder transactions executed on this thread.

Parameters
workSource Int: The original UID responsible for the binder call.
Return
Long token to restore original work source.

transact

Added in API level 1
fun transact(
    code: Int,
    data: Parcel,
    reply: Parcel?,
    flags: Int
): Boolean

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: This value cannot be null.
reply Parcel?: This value may be null.
flags Int: Additional operation flags. Either 0 for a normal RPC, or FLAG_ONEWAY for a one-way RPC.
Return
Boolean Returns the result from Binder#onTransact. A successful call generally returns true; false generally means the transaction code was not understood. For a oneway call to a different process false should never be returned. If a oneway call is made to code in the same process (usually to a C++ or Rust implementation), then there are no oneway semantics, and false can still be returned.

unlinkToDeath

Added in API level 1
open fun unlinkToDeath(
    recipient: IBinder.DeathRecipient,
    flags: Int
): Boolean

Local implementation is a no-op.

Parameters
recipient IBinder.DeathRecipient: This value cannot be null.
Return
Boolean true if the recipient is successfully unlinked, assuring you that its android.os.IBinder.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.
Exceptions
java.util.NoSuchElementException if the given recipient has not been registered with the IBinder, and the IBinder is still alive. Note that if the recipient was never registered, but the IBinder has already died, then this exception will not be thrown, and you will receive a false return value instead.

Protected methods

dump

Added in API level 1
protected open fun dump(
    fd: FileDescriptor,
    fout: PrintWriter,
    args: Array<String!>?
): Unit

Print the object's state into the given stream.

Parameters
fd FileDescriptor: The raw file descriptor that the dump is being sent to. This value cannot be null.
fout PrintWriter: The file to which you should dump your state. This will be closed for you after you return. This value cannot be null.
args Array<String!>?: additional arguments to the dump request. This value may be null.

onTransact

Added in API level 1
protected open fun onTransact(
    code: Int,
    data: Parcel,
    reply: Parcel?,
    flags: Int
): Boolean

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

Implementations that are returning a result should generally use Parcel.writeNoException and Parcel.writeException to propagate exceptions back to the caller.

Parameters
code Int: The action to perform. This should be a number between FIRST_CALL_TRANSACTION and LAST_CALL_TRANSACTION.
data Parcel: Marshalled data being received from the caller. This value cannot be null.
reply Parcel?: If the caller is expecting a result back, it should be marshalled in to here. This value may be null.
flags Int: Additional operation flags. Either 0 for a normal RPC, or FLAG_ONEWAY for a one-way RPC.
Return
Boolean Return true on a successful call; returning false is generally used to indicate that you did not understand the transaction code.