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.
sync
functionality if app is closed (because:sync
process is common shared one and is working in background)? "The attribute android:exported="true" allows processes other than your app (including the system) to access the Service. The attribute android:process=":sync" tells the system to run the Service in a global shared process named sync." - developer.android.com/training/sync-adapters/… – Trouper