How to send event from Service to Activity with Otto event bus?

后端 未结 7 1771
心在旅途
心在旅途 2020-12-02 05:19

Simple BusProvider.getInstance().post() bring exception not main thread. How to send event from Service to Activity with Otto event bus?

7条回答
  •  我在风中等你
    2020-12-02 06:15

    Best implementation custom bus 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));
            }
        }
    }
    

提交回复
热议问题