java 示例Otto事件总线提供程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 示例Otto事件总线提供程序相关的知识,希望对你有一定的参考价值。

package com.foo.bar.utils;

import android.os.Handler;
import android.os.Looper;

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;


/**
 * Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means such as through
 * injection directly into interested classes.
 */
public final class BusProvider {

    private static final MainPostingBus BUS = new MainPostingBus();

    public static MainPostingBus getInstance() {
        return BUS;
    }

    private BusProvider() {}

    public static class MainPostingBus extends Bus {

        private final Handler mainThread = new Handler( Looper.getMainLooper() );

        public MainPostingBus() {
            super( ThreadEnforcer.ANY );
        }

        @Override
        public void post( final Object event ) {
            if ( Looper.myLooper() == Looper.getMainLooper() ) {
                super.post( event );
            }
            else {
                mainThread.post( new Runnable() {
                    @Override
                    public void run() {
                        post( event );
                    }
                });
            }
        }
    }
}

以上是关于java 示例Otto事件总线提供程序的主要内容,如果未能解决你的问题,请参考以下文章

事件总线框架-OTTO

其他进程可以看到总线事件吗?

RuntimeException:无法调度事件@otto

Android Otto框架浅析

android事件总线(eventbus)设计与实现

java Android事件总线事件类示例。