问题
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
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()
andbindService()
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 needstartService()
orbindService()
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