温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中Binder机制的介绍和使用

发布时间:2021-06-26 14:52:20 阅读:189 作者:chen 栏目:互联网科技
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容介绍了“Android中Binder机制的介绍和使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

binder用于android进程间的通讯。客户端程序和系统服务,客户端进程之间,都是通过binder进行进程间通讯的。

1.客户端程序和系统服务

客户端和系统服务通讯的入口:cotext.getSystemService(String name)

android.app.ContextImpl:

 
  1. @Override      

  2. public Object getSystemService(String name) {      

  3. return SystemServiceRegistry.getSystemService(this, name);      

  4. }      

android.app.SystemServiceRegistry:

静态代码块注册系统服务:

 
  1. static {      

  2. registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,      

  3. new CachedServiceFetcher<AccessibilityManager>() {      

  4. @Override      

  5. public AccessibilityManager createService(ContextImpl ctx) {      

  6. return AccessibilityManager.getInstance(ctx);      

  7. }});      

  8. registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,      

  9. new CachedServiceFetcher<CaptioningManager>() {      

  10. @Override      

  11. public CaptioningManager createService(ContextImpl ctx) {      

  12. return new CaptioningManager(ctx);      

  13. }});      

  14. registerService(Context.ACCOUNT_SERVICE, AccountManager.class,      

  15. new CachedServiceFetcher<AccountManager>() {      

  16. @Override      

  17. public AccountManager createService(ContextImpl ctx) {      

  18. IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);      

  19. IAccountManager service = IAccountManager.Stub.asInterface(b);      

  20. return new AccountManager(ctx, service);      

  21. }});      

  22. ....      

ServiceManager、ServiceManagerNative:

serviceManager是管理系统服务的一个工具类。

 
  1. sServiceManager = ServiceManagerNative      

  2. .asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));      

getService方法:利用ServiceManagerProxy类通过进程间通讯的方式,获取其他服务在binder驱动中的binder对象mRemote。

xxx.Stub.asInterface(binder):

利用getService获取到的binder对象实例化相应服务的Proxy对象,返回给客户端,供客户端使用。

返回系统服务:

 
  1. /**      

  2. * Gets a system service from a given context.      

  3. */      

  4. public static Object getSystemService(ContextImpl ctx, String name) {      

  5. ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);      

  6. return fetcher != null ? fetcher.getService(ctx) : null;      

  7. }      

2.客户端之间

B应用进程调用A应用进程

B bindservice 向AmS请求启动B应用的service。启动service成功后会像AmS返回一个(binder驱动中的mRemote)binder,AmS会以该binder为参数调用ActivityThread类中的ApplicatonThread对象。接着会在ApplicationThread中回调conn接口。最后,B进程可以利用该binder调用A应用提供的功能。

“Android中Binder机制的介绍和使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/920274/blog/5044515

AI

开发者交流群×