I seem to have one of two issues. Any error, for example:
java.lang.IllegalStateException: Event bus [Bus "default"] accessed from non-main thread Looper
Or, if I managed to βaddressβ this, I simply never receive the event from my subscriber.
I currently have a class paved from several sources, a subclass of Class:
public class MainThreadBus extends Bus { private static Bus _bus; private Handler _handler = new Handler(Looper.getMainLooper()); public MainThreadBus() { if (_bus == null) { _bus = new Bus(); } } @Override public void register(Object obj) { _bus.register(obj); } @Override public void unregister(Object obj) { _bus.unregister(obj); } @Override public void post(final Object event) { if (Looper.myLooper() == Looper.getMainLooper()) { _bus.post(event); } else { _handler.post(new Runnable() { @Override public void run() { _bus.post(event); } }); } } }
Used as follows:
@Subscribe public void requestProgressAvailable(RESTRequestProgress progress) { processProgress(progress); } @Override protected void onResume() { super.onResume(); _bus = new MainThreadBus(); _bus.register(this); } @Override protected void onPause() { super.onPause(); _bus = new MainThreadBus(); _bus.unregister(this); }
And posting from the IntentService like this:
_bus = new MainThreadBus(); _bus.post(request.createRESTRequestProgress(RESTRequest.STATUS_STARTED));
And messages are not accepted. An alternate configuration made me get a stream error, so I'm going with this for now.
So what am I missing or is something wrong?
EDIT: Thanks to Andy below for pointing out that my code is not working as I thought. The above code now reflects changes based on this feedback.
source share