现在的位置: 首页 > 综合 > 正文

译Android.os.IBinder(待续)

2012年04月18日 ⁄ 综合 ⁄ 共 10575字 ⁄ 字号 评论关闭

结构
继承关系
public interface IBinder

android.os.IBinder

子类及间接子类
间接子类

Binder
一个远程对象的基类,IBinder定义一个轻量级的远程过程调用机制的核心部分。

 

类概述
Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls. This interface describes the abstract protocol for interacting with a remotable object. Do not implement this interface directly, instead extend from Binder.
远程对象的基本接口,在执行进程内或者进程间调用时,为高性能而设计的一个轻量级远程调用机制的核心部分。此接口描述了同远程对象交互的抽象协议。并没有直接实现该接口,而是从Binder类派生。
The key IBinder API is transact() matched by Binder.onTransact(). These methods allow you to send a call to an IBinder object and receive a call coming in to a Binder object, respectively. This transaction API is synchronous, such that a call to transact() does not return until the target has returned from Binder.onTransact(); this is the expected behavior when calling an object that exists in the local process, and the underlying inter-process communication (IPC) mechanism ensures that these same semantics apply when going across processes.
IBinder一个重要的API是transact(),同Binder.onTransact()相呼应。这两个方法允许你依次给IBinder对象发送一个调用,并接收一个传给Binder对象的调用。这个事务API是同步的,直到目标从Binder.onTransact()返回,调用的transact()才会返回;在调用一个本地进程已经存在对象的行为是可预见的,并且基于进程间通讯(IPC)的底层机制确保跨进程时也一样。
The data sent through transact() is a Parcel, a generic buffer of data that also maintains some meta-data about its contents. The meta data is used to manage IBinder object references in the buffer, so that those references can be maintained as the buffer moves across processes. This mechanism ensures that when an IBinder is written into a Parcel and sent to another process, if that other process sends a reference to that same IBinder back to the original process, then the original process will receive the same IBinder object back. These semantics allow IBinder/Binder objects to be used as a unique identity (to serve as a token or for other purposes) that can be managed across processes.
通过transact()发送的数据是Parcel,一个普通缓冲区,带有描述其内容的元数据。元数据常用来管理缓冲里的IBinder对象引用,这样就能使那些引用可以由缓冲区传给另一个进程。这个机制确保IBinder被写入Parcel,并且能够发送给另一个进程,如果另一个进程把同一个IBinder的引用回发到原先的进程,那么原先的进程就会接收到同一个IBinder对象。这个机制使IBinder/Binder对象作为一个唯一标识(作为一个记号或者其他用途)在进程间被管理着。
The system maintains a pool of transaction threads in each process that it runs in. These threads are used to dispatch all IPCs coming in from other processes. For example, when an IPC is made from process A to process B, the calling thread in A blocks in transact() as it sends the transaction to process B. The next available pool thread in B receives the incoming transaction, calls Binder.onTransact() on the target object, and replies with the result Parcel. Upon receiving its result, the thread in process A returns to allow its execution to continue. In effect, other processes appear to use as additional threads that you did not create executing in your own process.
系统为每个进程保留在其里面运行的事务线程池。这些线程常被用来分派处理所有来自于其他进程的IPCs(进程间调用)。例如,当一个IPC被用来从进程A发给进程B,进程A发送调用的线程在transact()中阻塞,因为它发送了事务给进程B。接下来,进程B有效的池线程接收进入的事务,调用目的对象的Binder.onTransact(),并且回复以结果Parcel。进程A的线程一接收到它的结果就返回以使它继续运行。实际上,另一个进程好像是是当前进程额外的线程一样,但它并不是在你的进程中创建执行的。
The Binder system also supports recursion across processes. For example if process A performs a transaction to process B, and process B while handling that transaction calls transact() on an IBinder that is implemented in A, then the thread in A that is currently waiting for the original transaction to finish will take care of calling Binder.onTransact() on the object being called by B. This ensures that the recursion semantics when calling remote binder object are the same as when calling local objects.
Binder系统也支持跨进程递归调用。例如,如果进程A(通过执行自己的IBinder的transaction())分配一项事务给进程B,并且进程B在(其Binder.onTransaction()中)处理事务时,又用transact()向进程A发送调用,那么进程A中的线程在等待原先的事务完成的同时,还会用Binder.onTransact()响应B的调用。这确保了在调用远程binder对象时的递归规则与调用本地对象时是一致的。
When working with remote objects, you often want to find out when they are no longer valid. There are three ways this can be determined:
• The transact() method will throw a RemoteException exception if you try to call it on an IBinder whose process no longer exists.
• The pingBinder() method can be called, and will return false if the remote process no longer exists.
• The linkToDeath() method can be used to register a IBinder.DeathRecipient with the IBinder, which will be called when its containing process goes away.
当操作远程对象时,你会经常想要查看它们是否有效。有三种方法可以使用:
 用函数transact()尝试调用IBinder时,如果它所在的进程不存在,则会抛出异常RemoteException。
 函数pingBinder()被调用时,如果远程进程不存在,则会返回false。
 函数linkToDeath()可被用作注册IBinder的IBinder.DeathRecipient,它可在含有此IBinder的进程退出的时候被调用。

参考
Binder

概述

内部类
interface IBinder.DeathRecipient
Interface for receiving a callback when the process hosting an IBinder has gone away.
当含有IBinder的进程退出时用以接收回调的接口

常量
int DUMP_TRANSACTION
IBinder protocol transaction code: dump internal state.
IBinder协议执行码:垃圾处理的内部状态码。
int FIRST_CALL_TRANSACTION
The first transaction code available for user commands.
用户命令的首个有效执行码
int FLAG_ONEWAY
Flag to transact(int, Parcel, Parcel, int): this is a one-way call, meaning that the caller returns immediately, without waiting for a result from the callee.
transact(int, Parcel, Parcel, int)方法的标示:这是一种单向调用,意思就是说调用者不等待被调用者的结果就立即返回。
int INTERFACE_TRANSACTION
IBinder protocol transaction code: interrogate the recipient side of the transaction for its canonical interface descriptor.
IBinder协议执行码:询问执行调用接收方的规范接口描述符。
int LAST_CALL_TRANSACTION
The last transaction code available for user commands.
用户命令的最后的有效执行码。
int LIKE_TRANSACTION
IBinder protocol transaction code: tell an app asynchronously that the caller likes it.
IBinder协议执行码:异步的告知应用程序调用者喜欢它。
int PING_TRANSACTION
IBinder protocol transaction code: pingBinder().
IBinder协议执行码:pingBinder()
int TWEET_TRANSACTION
IBinder protocol transaction code: send a tweet to the target object.
IBinder协议执行码:发送tweet给目标对象

公共方法
abstract void dump(FileDescriptor fd, String[] args)
Print the object's state into the given stream.
打印对象状态到给予的流。
abstract void dumpAsync(FileDescriptor fd, String[] args)
Like dump(FileDescriptor, String[]) but always executes asynchronously.
类似于dump(FileDescriptor, String[]),但总是异步执行。

abstract String
getInterfaceDescriptor()
Get the canonical name of the interface supported by this binder.
获取由binder支持的接口规范的名称。
abstract boolean isBinderAlive()
Check to see if the process that the binder is in is still alive.
检查进程中binder是否处于活动状态
abstract void linkToDeath(IBinder.DeathRecipient recipient, int flags)
Register the recipient for a notification if this binder goes away.
如果binder退出,注册到通知接收方。
abstract boolean pingBinder()
Check to see if the object still exists.
检查对象是否退出。
abstract IInterface
queryLocalInterface(String descriptor)
Attempt to retrieve a local implementation of an interface for this Binder object.
试图为Binder对象检索接口的本地实现。
abstract boolean transact(int code, Parcel data, Parcel reply, int flags)
Perform a generic operation with the object.
执行对象的通用操作。
abstract boolean unlinkToDeath(IBinder.DeathRecipient recipient, int flags)
Remove a previously registered death notification.
移除之前注册的死亡通知。

常量
public static final int DUMP_TRANSACTION
Since: API Level 1
IBinder protocol transaction code: dump internal state.
IBinder协议执行码:垃圾处理的内部状态码。
常量值:1598311760 (0x5f444d50)
public static final int FIRST_CALL_TRANSACTION
Since: API Level 1
The first transaction code available for user commands.
用户命令的首个有效执行码。
常量值:1 (0x00000001)
public static final int FLAG_ONEWAY
Since: API Level 1
Flag to transact(int, Parcel, Parcel, int): this is a one-way call, meaning that the caller returns immediately, without waiting for a result from the callee. Applies only if the caller and callee are in different processes.
transact(int, Parcel, Parcel, int)方法的标示:这是一种单向调用,意思就是说调用者不等待被调用者的结果就立即返回。仅适用于调用者和被调用者是分属不同的进程。
常量值: 1 (0x00000001)
public static final int INTERFACE_TRANSACTION
Since: API Level 1
IBinder protocol transaction code: interrogate the recipient side of the transaction for its canonical interface descriptor.
IBinder协议执行码:询问执行调用接收方的规范接口描述符。

常量值:1598968902 (0x5f4e5446)
public static final int LAST_CALL_TRANSACTION
Since: API Level 1
The last transaction code available for user commands.
用户命令的最后有效执行码。
常量值:16777215 (0x00ffffff)
public static final int LIKE_TRANSACTION
Since: API Level 15
IBinder protocol transaction code: tell an app asynchronously that the caller likes it. The app is responsible for incrementing and maintaining its own like counter, and may display this value to the user to indicate the quality of the app. This is an optional command that applications do not need to handle, so the default implementation is to do nothing.
There is no response returned and nothing about the system will be functionally affected by it, but it will improve the app's self-esteem.
IBinder协议执行码:异步的告知应用程序调用者喜欢它。应用程序会像计数器一样负责累加和维护其本身,并且可以将表示应用质量的值显示给用户。这是一个可选的命令,应用不必处理,所以默认的实现没有作任何处理。
常量值: 1598835019 (0x5f4c494b)
public static final int PING_TRANSACTION
Since: API Level 1
IBinder protocol transaction code: pingBinder().
IBinder协议执行码:pingBinder()。
常量值:1599098439 (0x5f504e47)
public static final int TWEET_TRANSACTION
Since: API Level 13
IBinder protocol transaction code: send a tweet to the target object. The data in the parcel is intended to be delivered to a shared messaging service associated with the object; it can be anything, as long as it is not more than 130 UTF-8 characters to conservatively fit within common messaging services. As part of HONEYCOMB_MR2, all Binder objects are expected to support this protocol for fully integrated tweeting across the platform. To support older code, the default implementation logs the tweet to the main log as a simple emulation of broadcasting it publicly over the Internet.
Also, upon completing the dispatch, the object must make a cup of tea, return it to the caller, and exclaim "jolly good message old boy!".

IBinder协议执行码:发送tweet给目标对象。打包的数据将被传送到与对象关联的共享消息服务中;它可能是任何东西,只要它少于130个UTF-8字节被谨慎的保存在普通的消息服务里。作为HONEYCOMB_MR2的一部分,预计所有的Binder对象将支持跨平台完全整合的tweet协议。为了支持旧的代码,默认的实现记录tweet消息到主日志中作为在互联网上广播的一种简单模拟。
此外,调度完成后,这个对象应该冲一杯茶,将它返回给调用者,并大声的宣布:“非常棒的信息老男孩!”。

常量值:1599362900 (0x5f545754)

公共方法
public abstract void dump (FileDescriptor fd, String[] args)
Since: API Level 3
Print the object's state into the given stream.
打印对象状态到给定的流。
参数
fd The raw file descriptor that the dump is being sent to.
发送dump给的原始文件描述符。
args additional arguments to the dump request.
dump请求的额外参数。

抛出异常
RemoteException

public abstract void dumpAsync (FileDescriptor fd, String[] args)
Since: API Level 13
Like dump(FileDescriptor, String[]) but always executes asynchronously. If the object is local, a new thread is created to perform the dump.
类似于dump(FileDescriptor, String[]),但是一直异步执行。如果对象处于本地,将创建新线程执行dump。
参数
fd The raw file descriptor that the dump is being sent to.
发送dump给的原始文件描述符。
args additional arguments to the dump request.
dump请求的额外参数。
抛出异常
RemoteException

public abstract String getInterfaceDescriptor ()
Since: API Level 1
Get the canonical name of the interface supported by this binder.
获取由binder支持的接口规范的名称。
抛出异常
RemoteException

public abstract boolean isBinderAlive ()
Since: API Level 1
Check to see if the process that the binder is in is still alive.
检查进程的binder是否处于活动状态。
返回
• false if the process is not alive. Note that if it returns true, the process may have died while the call is returning.
• 如果进程处于非活动状态则返回false。注意如果返回true,进程可能在调用正在返回时已经死掉了。
public abstract void linkToDeath (IBinder.DeathRecipient recipient, int flags)
Since: API Level 1
Register the recipient for a notification if this binder goes away. If this binder object unexpectedly goes away (typically because its hosting process has been killed), then the given IBinder.DeathRecipient's DeathRecipient.binderDied() method will be called.
如果binder退出,注册到通知接收方。如果binder对象意外的退出(典型的情况可能是因为它的宿主进程被杀掉了),那么给定的IBinder.DeathRecipient的方法DeathRecipient.binderDied()会被调用。
You will only receive death notifications for remote binders, as local binders by definition can't die without you dying as well.
你将只接收远程binders的死亡通知,同样按照定义IBinder没有死本地binders也不能死亡。

抱歉!评论已关闭.