IPC in Android using GreenRobot eventbus
Asked Answered
A

3

9

I need to communicate with a remote service, using (greenrobot) EventBus. Unfortunately, it does not seem to work with IPC. Looking at the code, I don't see a workaround either. Any help would be appreciated !

Bonus question - are there any other EventBuses (for Android) which support IPC ?

Abound answered 20/4, 2014 at 22:44 Comment(0)
M
7

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
Marksman answered 20/4, 2014 at 23:1 Comment(3)
Thanks for your reply. As I mentioned, I am using a "remote service", and want to some data to be communicated to multiple apps. For now the consumer apps are polling the producer (using AIDL); but I wanted to make it asynchronous. Have I taken a wrong approach ?Abound
@Raj: Use broadcast Intents to notify consumer apps about changes.Marksman
Thanks again ! Since Eventbus (greerobot) claims itself to be better than android broadcast system (refer to their FAQ)- I was wondering if they do support IPC. It seems they do not support, so I will fallback to broadcasts.Abound
P
5

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:

public class Listener implements IIpcEventBusConnectionListener, IIpcEventBusObserver {

    public Listener() {
        String targetApp = "com.packagename";
        IIpcEventBusConnector connector =
            ConnectorFactory.getInstance().buildConnector(context, this, targetApp);
        connector.startConnection();
    }

    @Override
    public void onConnected(IIpcEventBusConnector connector) {
        connector.registerObserver(this);
    }

    @Override
    public void onEvent(IEventIpc event) {
        Log.d("ipceventbus", "Received event: " + event.getClass());
    }

    @Override
    public void onDisconnected(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.

Parlous answered 19/9, 2016 at 6:37 Comment(0)
E
0

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

Egwin answered 20/3, 2017 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.