美文网首页
Android Binder

Android Binder

作者: 狠狠狠努力的疯子 | 来源:发表于2019-11-14 16:30 被阅读0次

github地址:https://github.com/BraveFaith/BinderTest

1.如果Activity 和 Service 是相同进程,那么bind服务的时候,ServiceConnection的onServiceConnected方法返回的IBinder实际是Service内部实现的Stub对象,所以在调用Stub.asInterface(android.os.IBinder obj)方法的时候会调用Binder 的queryLocalInterface方法,这个方法会返回mOwner对象,而mOwner则是Service 实现的Stub对象,所以Activity最终拿到的是Service中实现的Stub对象.

2.如果Activity 和 Service 不在同一个进程时,那么bind服务的时候,ServiceConnection的onServiceConnected方法返回的IBinder实际是BinderProxy,所以在调用Stub.asInterface(android.os.IBinder obj)方法的时候会调用BinderProxy 的queryLocalInterface方法,这个方法默认就是返回null,所以Stub.asInterface(android.os.IBinder obj)方法会返回Proxy对象,所以Activity最终拿到的是Proxy对象

图1 图2 图3

相关文章

网友评论

      本文标题:Android Binder

      本文链接:https://www.haomeiwen.com/subject/vnxubctx.html