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
The Android Interface Definition Language (AIDL) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
Binder is an Android-specific interprocess communication mechanism, and remote method invocation system. That is, one Android process can call a routine in another Android process, using binder to indentify the method to invoke and pass the arguments between processes.
Binder IPC Framework in Android Framework enables a remote invocation of the methods in other processes. A client process communicate to another server process and can run the methods in other process as it is done locally and can get the required data from the server process.
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()
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With