ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## (一).前言: 前面我们已经对于AndroidAnnotations框架集成RoboGuice做了讲解,今天我们开始具体学习一下第三方框架集成Otto事件总线。Otto事件总线和我们经常使用Eventbus差不多。Otto 官网: [http://square.github.io/otto/](http://square.github.io/otto/),Otto框架的主要功能是帮助我们来降低多个类之间的耦合度的(解耦)。 FastDev4Android框架项目地址:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android)  ## (二).集成Otto和AndroidAnnotations * 集成AndroidAnnotations框架到项目中(具体方法见:[【FastDev4Android框架开发】AndroidAnnnotations注入框架介绍和Android Studios基本配置(七)](http://blog.csdn.net/developer_jiangqq/article/details/49468923) * 集成otto框架到项目中(具体请查看[http://square.github.io/otto/](http://square.github.io/otto/))这边讲一下AndroidStudio配置吧: dependencies {   compile 'com.squareup:otto:1.3.8' } *  使用@EBean来为事件总线创建一个单例类 *  创建通过事件总线传输分发的事件类 * 使用bus.post()来进行发送事件到事件总线中 * 使用@Subscribe进行注解来获取发布的事件 下面的实例表示Fragment进行通知Activity标题发生更新: ~~~ // Declare the busas an enhanced bean @EBean(scope =Scope.Singleton) public class OttoBusextends BasicBus { } public classUpdateTitleEvent { public final String title; public UpdateTitleEvent(String title) { this.title = title; } } ~~~ ~~~ @EActivity(R.layout.hello_activity) public classHelloAndroidActivity extends FragmentActivity { @Bean OttoBus bus; @Override protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); bus.register(this); } @Override protected void onDestroy() { super.onDestroy(); bus.unregister(this); } @Subscribe public void onUpdateTitle(UpdateTitleEventevent) { setTitle(event.title); } } ~~~ ~~~ @EFragment(R.layout.hello_fragment) public classHelloFragment extends Fragment { int counter = 1; @Bean OttoBus bus; @Click void fragmentButtonClicked() { bus.post(newUpdateTitleEvent("Clicks: " + counter++)); } } ~~~ 到此位置关于AndroidAnnotations第三方框架集成之Otto集成已经全部讲解完成了。 同时FastDev4Android项目已经添加配置了AndroidAnnotations框架,后期的框架项目中也会主要使用这个DI框架,.欢迎大家去Github站点进行clone或者下载浏览:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android) 同时欢迎大家star和fork整个开源快速开发框架项目~