首页 > 编程知识 正文

warnier(binder原理)

时间:2023-05-05 02:09:31 阅读:68540 作者:3246

Binder、IBinder和接口的关系IBinder接口Binder实现了IBinder接口。

publicclassbinderimplementsibinder { . } I binder接口定义了一些常量和方法。 例如,第一个交易代码(transaction code )和最后一个交易代码。

最小的交易代码为1,最大的交易代码为16 ^ 6 - 1=16777215。 其他事务代码介于这两个值之间。 可以看到最多有16777215个翻译。

/* * thefirsttransactioncodeavailableforusercommands.*/int first _ call _ transaction=0x 0000001;/* * thelasttransactioncodeavailableforusercommands.*/intlast _ call _ transaction=0x 00 ffffff; IBinder界面还定义了isBinderAlive、queryLocalInterface、transact、linkToDeath和unlinkToDeath等方法。

/* * checktoseeiftheprocessthatthebinderisinisstillalive.* * @ returnfalseiftheprocessisnotalive.notethatifitreturns * */* * attempttoretrievealocalimplementationofaninterface * forthisbinderobject.ifnullisreturned, you will need * toinstantiateaproxyclasstomarshallcallsthrough * the transact (method.*/public @ nulllableiinterfacequeryloct @NonNull Parcel data,@Nullable Parcel reply,int flags ) throws RemoteException; publicvoidlinktodeath (@ nonnulldeathrecipientrecipient,int flags ) throws远程执行; publicbooleanunlinktodeath (@ nonnulldeathrecipientrecipient,int flags ); IBinder接口还定义了一个子接口,DeathRecipient表示服务进程已消失的回调。

/* * interfaceforreceivingacallbackwhentheprocesshostinganibinder * hasgoneaway.* * @ see # link to death */publicinterfate

publicinterfaceicustomaidlextendsandroid.OS.interface { . }接口只有一种方法,即asBinder。

公共I binder as binder (; asBinder ()方法用于返回AIDL接口中Stub类的对象。 如果是同一进程的本地接口,则为this。 否则就是BinderProxy代理对象。

/* *本地- sideipcimplementationstubclass.*/publicstaticabstractclassstubextendsandroid.OS.binderimplementscom.Cao Shen.Android sample.binder.icustomaidl { .

/* * castanibinderobjectintoancom.Cao Shen.Android sample.binder.icustomaidlinterface,* generatingaproxyifneded.* * } Android.OS.iinterfaceiin=obj.querylocalinterface (descriptor ); if () ) (iin!=null (iininstanceofcom.Caoshen.Androidsample.binder.icustomaidl ) (return ).Cao Shen.Android sample ) )。 }如果return newcom.Cao Shen.Android sample.binder.icustomaidl.stub.proxy (obj ) queryLocalInterface获得了本地接口,则否则,将Binder自身传递给BinderProxy的作为mRemote返回一个BinderProxy对象。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。