Android IPC LocalSocket与Binder(AIDL) [英] Android ipc LocalSocket vs Binder (AIDL)

查看:227
本文介绍了Android IPC LocalSocket与Binder(AIDL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每个应用程序都能够将数据发送到我的服务.因此,我需要进行进程间通信.我可以找到的每个页面都建议使用Messenger,AIDL或Intents(BroadcastReceiver).到目前为止,我可以通过构建一些测试应用程序来弄清楚的是,如果在while(true)循环中发送多个线程,则BroadcastReceiver的运行速度非常慢,消息可能会丢失而不会发出通知. AIDL和Messenger不仅实现起来很复杂(需要服务,活页夹等),而且还可能提供奇怪的行为,例如当发送多个线程导致AIDL发生RemoteException(!!! FAILED BINDER TRANSACTION !!!)时使用单向关键字.我要确保有保证的交货.在应保证交付时,甚至还有理由使用oneway吗?

I want every app to be able to send data to my service. Therefore I need inter process communication. Every page I can find proposes to use Messenger, AIDL or Intents (BroadcastReceiver). So far what I could figure out by building some test apps is that BroadcastReceiver is extremely slow and messages can get lost without notification if sending with multiple threads inside while(true) loop. AIDL and Messenger are not only complicated to implement (service is needed, binder,...) but can provide strange behavior for example when sending with multiple threads resulting in RemoteException (!!! FAILED BINDER TRANSACTION !!! ) with AIDL just when using oneway keyword. I want to ensure that delivery is guaranteed. Is there even a reason to use oneway when delivery should be guaranteed?

尽管如此,LocalSocket似乎非常易于使用(不需要服务,就像Java套接字一样工作).客户端应用可以打开一个LocalSocket,连接到提供的地址,然后while(true) outputstream.writeObject();

Nevertheless, LocalSocket seems to be extremely easy to use (no need for a service, works just like java socket). Client apps could just open a LocalSocket, connect to the provided address and then while(true) outputstream.writeObject();

使用LocalSocket时有什么缺点,因为android开发人员页面上说:某些应用尝试使用传统的Linux技术(例如网络套接字和共享文件)来实现IPC.我们强烈建议您改为使用Android系统功能来实现IPC",但是对此不作进一步评论

Are there any downsides when using LocalSocket because the android developer page says "Some apps attempt to implement IPC using traditional Linux techniques such as network sockets and shared files. We strongly encourage you to instead use Android system functionality for IPC" but does not further comment on this

推荐答案

使用LocalSocket时有什么缺点

Are there any downsides when using LocalSocket

  1. LocalSocket在框架级别没有安全性.尽管您可能希望每个应用程序都能够将数据发送到我的服务",但用户可能不会,这就是为什么标准IPC可以受到权限保护的原因.

  1. There is no security at the framework level for LocalSocket. While you may "want every app to be able to send data to my service", the user may not, which is why standard IPC can be protected by permissions.

startService()bindService()将导致创建服务的实例,甚至在需要时为您启动一个进程,以处理请求.您的服务将不会一直运行.因此,您仍然需要startService()bindService().

startService() and bindService() will cause an instance of your service to be created, even starting a process for you, if needed to handle the request. Your service will not be running all of the time. So, you need startService() or bindService() anyway.

这篇关于Android IPC LocalSocket与Binder(AIDL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆