让服务中的订阅者对使用otto eventbus的活动中发布的事件做出反应

时间:2015-01-14 09:04:22

标签: android otto

我的app中有启动器活动,应该在onCreate()中使用otto事件总线触发事件。活动本身确保启动服务。总线通过总线派生类的BusProvider实现为单例。每当我从我的活动发布到总线时,活动本身就会收到事件(我订阅了调试目的)而不是服务,尽管它应该都在主线程上发送。

因为我在活动中收到活动,所以基本上接缝工作。我假设服务(背景)和活动(ui)之间的差异可能是我没有在服务中收到事件的原因,但为什么呢?

以下是My derived Bus Class的代码:

public class MainBus extends Bus {
    private final Handler handler = new Handler(Looper.getMainLooper());

    public MainBus(ThreadEnforcer any) {
        super(any);
    }

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

这是BusProvider Singleton

public final class BusProvider {
    // Also tried with ThreadEnforcer.MAIN 
    private static final MainBus BUS = new MainBus(ThreadEnforcer.ANY);

    public static MainBus getInstance() {
        return BUS;
    }

    private BusProvider() {
        // No instances.
    }
}

以下是我的活动的相关代码

public class Splashscreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BusProvider.getInstance().register(this);

        // Start the Service if not yet started.
        if (!isMyServiceRunning()) {
            startMyService();
        }

        LogProvider.getInstance().d("Splashscreen", "Activity started.");
        BusProvider.getInstance().post(new SplashscreenStartupCompletedEvent());
        LogProvider.getInstance().d("Splashscreen",
                "After Startup Event posted");

        setContentView(R.layout.splashscreen);
    }

    /**
     * private function to check if the service is running
     * 
     * @return boolean True if the service is running, false otherwise
     */
    private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager
                .getRunningServices(Integer.MAX_VALUE)) {
            if (MyApplicationService.class.getName().equals(
                    service.service.getClassName())) {
                LogProvider.getInstance().i("Splashscreen",
                        "Service is already running.");
                return true;
            }
        }
        LogProvider.getInstance().n("Splashscreen", "Service is not running.");
        return false;
    }

    /**
     * private function to start the Backgroundservice
     */
    private void startMyService() {
        LogProvider.getInstance().i("Splashscreen", "Service will be started.");
        Context context = getApplicationContext();
        Intent service = new Intent(
                context,
                com.classpath.MyApplicationService.class);
        context.startService(service);
    }

    @Subscribe
    public void afterStartEvent(SplashscreenStartupCompletedEvent event) {
        LogProvider.getInstance().d("afterStartEvent", "Event received");
    }
}

服务的相关部分

public class MyApplicationService extends Service {
    @Override
    public void onCreate() {
        BusProvider.getInstance().register(this);       
        super.onCreate();
    }

    @Subscribe
    public void afterStartupCompleted(SplashscreenStartupCompletedEvent event) {
        LogProvider.getInstance().d("MyApplicationService",
                "StartupCompletedEvent empfangen");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

0 个答案:

没有答案