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.
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.
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.
I know the answer was already accepted but I thought I'd write about how I solved the problem in case anyone runs across this and is curious how the code might look like.
If you are using Otto, I followed the accepted answer above by removing the android:process
from the manifest. I also followed the answer provided here How to send event from Service to Activity with Otto event bus? , where a Bus exception was being thrown for not being run on the main thread. Therefore I combined the two answers and created a bus to be executed on the main thread as per the link above.
public class MainThreadBus extends Bus {
private final Handler mHandler = new Handler(Looper.getMainLooper());
@Override
public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
super.post(event);
} else {
mHandler.post(new Runnable() {
@Override
public void run() {
MainThreadBus.super.post(event);
}
});
}
}
}
I then created a Bus singleton that can used anywhere in the application:
public final class BusProvider {
private static final MainThreadBus BUS = new MainThreadBus();
public static MainThreadBus getInstance() {
return BUS;
}
private BusProvider() {
}
}
In my SyncAdapter I used the following code initiate the event, BusProvider.getInstance().post(event);
and in my application fragment I simply subscribed to the event.
This worked perfectly fine when the application was in the foreground as well as when the sync adapter was initiated in the background after the application was swiped away.
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));
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.