Ipc In Android Using Greenrobot Eventbus
Solution 1:
I need to communicate with a remote service, using (greenrobot) EventBus.
The entire point of greenrobot's EventBus, like Square's Otto and LocalBroadcastManager
, is to not use IPC.
Any help would be appreciated !
Don't use greenrobot's EventBus for IPC. Use one of Android's myriad IPC mechanisms for IPC:
startActivity()
startActivityForResult()
startService()
bindService()
sendBroadcast()
and its variations (e.g.,sendOrderedBroadcast()
)- a
ContentProvider
Solution 2:
There is an IPC EventBus option which allows you to send events over IPC. https://github.com/NewtronLabs/IpcEventBus
According to the documentation all you have to do to get an event is this:
publicclassListenerimplements IIpcEventBusConnectionListener, IIpcEventBusObserver {
publicListener() {
String targetApp = "com.packagename";
IIpcEventBusConnector connector =
ConnectorFactory.getInstance().buildConnector(context, this, targetApp);
connector.startConnection();
}
@OverridepublicvoidonConnected(IIpcEventBusConnector connector) {
connector.registerObserver(this);
}
@OverridepublicvoidonEvent(IEventIpc event) {
Log.d("ipceventbus", "Received event: " + event.getClass());
}
@OverridepublicvoidonDisconnected(IIpcEventBusConnector connector) {
}
}
And on the other side you post the event like this:
IpcEventBus.getInstance().postEvent(new MyEvent());
I created a two apps and they were able to send events to each other.
Solution 3:
Another library that follows the EventBus syntax more closely is HermesEventBus. It supports IPC (and intra process) both.
Although they should have just derived from EventBus, so that we can just inject EventBus object (which is actually a HermesEventBus), and not have to update code everywhere. https://github.com/eleme/HermesEventBus
Post a Comment for "Ipc In Android Using Greenrobot Eventbus"