简体   繁体   English

使用GreenRobot事件总线的Android中的IPC

[英]IPC in Android using GreenRobot eventbus

I need to communicate with a remote service, using (greenrobot) EventBus. 我需要使用(greenrobot)EventBus与远程服务进行通信 Unfortunately, it does not seem to work with IPC. 不幸的是,它似乎不适用于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 ? 奖金问题 - 是否还有支持IPC的其他EventBuses(适用于Android)?

I need to communicate with a remote service, using (greenrobot) EventBus. 我需要使用(greenrobot)EventBus与远程服务进行通信。

The entire point of greenrobot's EventBus, like Square's Otto and LocalBroadcastManager , is to not use IPC. greenobot的EventBus的全部内容,如Square的Otto和LocalBroadcastManager ,是使用IPC。

Any help would be appreciated ! 任何帮助,将不胜感激 !

Don't use greenrobot's EventBus for IPC. 不要将greenrobot的EventBus用于IPC。 Use one of Android's myriad IPC mechanisms for IPC: 使用Android的无数IPC机制之一:

  • startActivity()
  • startActivityForResult()
  • startService()
  • bindService()
  • sendBroadcast() and its variations (eg, sendOrderedBroadcast() ) sendBroadcast()及其变体(例如sendOrderedBroadcast()
  • a ContentProvider ContentProvider

There is an IPC EventBus option which allows you to send events over IPC. 有一个IPC EventBus选项,允许您通过IPC发送事件。 https://github.com/NewtronLabs/IpcEventBus 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. 我创建了两个应用程序,他们能够相互发送事件。

Another library that follows the EventBus syntax more closely is HermesEventBus. 另一个遵循EventBus语法的库是HermesEventBus。 It supports IPC (and intra process) both. 它支持IPC(和内部处理)。

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. 虽然它们应该刚刚从EventBus派生,所以我们可以只注入EventBus对象(实际上是一个HermesEventBus),而不必在任何地方更新代码。 https://github.com/eleme/HermesEventBus https://github.com/eleme/HermesEventBus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM