Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Otto/EventBus across multiple processes

Is it possible to post event in one process (for example inside SyncAdapter which has android:process=":sync" manifest attribute) and receive it in another (inside regular app UI) with Otto or EventBus?

I know that Intent and BroadcastReceiver work just fine for communication across multiple processes but I would like to have simplicity and flexibility with Otto/EventBus.

like image 813
svenkapudija Avatar asked Aug 29 '14 12:08

svenkapudija


3 Answers

No, that is not possible, as Otto, greenrobot's EventBus, and LocalBroadcastManager are all in-process solutions.

You might consider simply removing the android:process attribute from the manifest, so it all runs in one process.

like image 75
CommonsWare Avatar answered Oct 25 '22 05:10

CommonsWare


No, but you can use transit. For example using BroadcastReceiver: In one process, send a broadcast with your data, then through the interior of BroadcastReceiver onReceive methods, post a otto event.

Like my codes:

public class ReceiveMessageBroadcastReceiver extends BroadcastReceiver {

    public static final String ACTION_RECEIVE_MESSAGE
            = "me.drakeet.xxxxxx.ACTION_RECEIVE_MESSAGE";
    public static final String AGR_MESSAGE = "AGR_MESSAGE";


    // this method can be called in other processes
    public static void sendBroadcast(Context context, MessageContent content) {
        Intent intent = new Intent();
        intent.setAction(ACTION_RECEIVE_MESSAGE);
        intent.putExtra(AGR_MESSAGE, content);
        context.sendBroadcast(intent);
    }


    // this method will run in your default process, so you can post otto events to your
    // default process
    @Override public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ACTION_RECEIVE_MESSAGE)) {
            MessageContent content = intent.getParcelableExtra(AGR_MESSAGE);
            Otto.getSeat().post(new PlayMessageReceivedEvent(content));
        }
    }
}
like image 1
drakeet Avatar answered Oct 25 '22 06:10

drakeet


I know this question is a bit old, but there seems to be a library that claims it can handle a cross-process communication with an event-bus/Rx style architecture.

https://github.com/edisonw/PennStation

Disclaimer: I have not tried this, just found it and it claims to do what this question is asking.

like image 1
pjco Avatar answered Oct 25 '22 06:10

pjco