Android ipc LocalSocket vs Binder (AIDL)

谁都会走 提交于 2021-01-21 08:20:11

问题


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?

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();

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


回答1:


Are there any downsides when using LocalSocket

  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.

  2. 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.




回答2:


AIDL: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service.

Binder: If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder.

Messenger: If you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger.



来源:https://stackoverflow.com/questions/38125066/android-ipc-localsocket-vs-binder-aidl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!