Junior — Middle
What methods are used for data exchange between separate processes?
sobes.tech AI
Answer from AI
Several methods are used for data exchange between separate processes (IPC) in Android:
- Intents — allow data transfer between components (Activity, Service) even in different processes.
- Content Providers — provide access to data and their exchange between applications through a standard interface.
- Binder — a low-level IPC mechanism in Android that allows calling methods of a remote object.
- Messenger — a wrapper around Binder for message exchange (Message) between processes.
- AIDL (Android Interface Definition Language) — used to define interfaces that can be called from another process.
Example of using Messenger for message exchange between processes:
// In the service, create a Handler and Messenger
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// handle message
}
}
Messenger messenger = new Messenger(new IncomingHandler());
// In the client, send a message via Messenger
Message msg = Message.obtain(null, MSG_CODE);
clientMessenger.send(msg);