Reputation: 3481
If I have an app and a service in an apk. The service though running in another process than the main app using android:process=":SomeService"
attribute in the tag -Can the service propagate data to the main app via for instance LiveData or LocalBroadcastManager which can be easily transmitted anywhere in a program, or do I have to use some IPC-based mechanism such as Intents or .aidl or whatever. I am hoping you will say there is an easy way to share data but I suspect you are going to say that because they are in different processes there is no easy way to do it. I would like to avoid all the overhead with Intents or .aidl or whatever... It should also be said that there is a special reason why I want the service in another process.
Upvotes: 0
Views: 263
Reputation: 95618
The only way to share data between 2 OS processes is to serialize/deserialize the data to transfer it between the OS processes.
You can use AIDL for this.
You can also send a broadcast Intent
(using sendBroadcast()
) if you want to send data from the Service
to the other components running in the other process. You cannot use LocalBroadcastManager
because the LocalBroadcastManager
doesn't send anything outside of its own OS process. Be aware that there are privacy and security considerations when sending a broadcast Intent
which may be read and/or intercepted by other applications.
Upvotes: 1
Reputation: 6345
Can the service propagate data to the main app via for instance LiveData or LocalBroadcastManager which can be easily transmitted anywhere in a program, or do I have to use some IPC-based mechanism such as Intents or .aidl or whatever
So if you have android:process
declared for your Service and If the name assigned to this attribute begins with a colon (':'), a new process, is created when it's needed and the service runs in that process.
You need to use AIDL to communicate with this service as this is another process as far as Android is concerned & On Android, one process cannot normally access the memory of another process.
So to talk to each other, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you.
I am hoping you will say there is an easy way to share data but I suspect you are going to say that because they are in different processes there is no easy way to do it
Well, this the way android process model works - i am afraid what else anyone else can say anything different
I would like to avoid all the overhead with Intents or .aidl or whatever... It should also be said that there is a special reason why I want the service in another process.
That you have a specific reason to run the service in another process - This makes a case to go for AIDL even more stronger, because as per official android docs,
Using AIDL is necessary if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger.
May be you can think of explore implementing Binder or Messenger - if that serves your specific service's implementation details.
Upvotes: 1