How to send an event from a service to activity using the Otto event bus?

Simple BusProvider.getInstance().post() include exception not main thread . How to send an event from a service to activity using the Otto event bus?

+57
android otto
Mar 15 '13 at 11:53
source share
7 answers

To post messages from any stream (main or background) and receive in the main stream, try something like

 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); } }); } } } 

Note: Credit refers to Jake Wharton and "pommedeterresaute" at https://github.com/square/otto/issues/38 for a general approach. I just implemented it with a wrapper class, not a subclass.

+119
Mar 15 '13 at 13:09
source share

To publish any thread (main or background) and get in the main thread, use MainThreadBus instead of Vanilla Bus

 public class MainThreadBus extends Bus { private final Handler handler = new Handler(Looper.getMainLooper()); @Override public void post(final Object event) { if (Looper.myLooper() == Looper.getMainLooper()) { super.post(event); } else { handler.post(new Runnable() { @Override public void run() { MainThreadBus.super.post(event); } }); } } } 

This is based on Andy Danny's answer.

There is no need to expand or wrap the Bus object, to do this or that. In Denny's answer, which is actually a wrapper, the Bus base class is used only as an interface, all functions are overwritten.

This would work even if you removed the base Bus class, unless you referenced MainThreadBus using the Bus link.

+19
Feb 27 '14 at 9:13
source share

bus = new bus (ThreadEnforcer.ANY); is a clear solution to this problem. That is all you have to do.

+2
Apr 04 '16 at 10:55 on
source share

Or just do it if you are sure that you are sending a message from the main thread:

 new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { mBus.post(new myEvent()); } }); 

As the saying goes, "Keep it simple and stupid" :)

+2
Nov 25 '16 at 4:06
source share

Best custom tire class for me

 public class AndroidBus extends Bus { private final Handler mainThread = new Handler(Looper.getMainLooper()); @Override public void post(final Object event) { if (Looper.myLooper() == Looper.getMainLooper()) { super.post(event); } else { mainThread.post(() -> AndroidBus.super.post(event)); } } } 
0
Apr 14 '16 at 10:31
source share

I made it simple:

 Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { bus.post(event); } }); 

Happy coding .. :)

0
Jan 19 '17 at 14:01
source share

Just create a BasicBus using ThreadEnforcer.NONE to send a message from non-main threads. Mentioned ThreadEnforcer.MAIN is the exact opposite (and by default), which only accepts messages from the main thread.

-one
Apr 22 '15 at 21:55
source share



All Articles