ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
## (一).前言: 前面我们已经对于AndroidAnnotations框架的线程处理做了讲解,今天我们开始具体学习一下第三方框架集成。 FastDev4Android框架项目地址:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android)  ## (二).RoboGuice集成 2.1.简介说明 自AndroidAnnotations1.0起,AndroidAnnotations已经完成集成了RoboGuice1.1.1。 自AndroidAnnotations3.1起,发布的RoboGuice1.0版本已经从RoboGuice2.0版本中移除了。 自AndroidAnnotations3.3起,从发布的RoboGuice3.0版本开始,该更新已经被支持。现在RoboGuice的最新版本是3.0。 ![](https://box.kancloud.cn/2016-01-18_569c8eb2ac94d.jpg) ![](https://box.kancloud.cn/2016-01-18_569c8eb2bd9df.jpg) 2.2.集成RoboGuice和AndroidAnnotations * 集成AndroidAnnotations框架到项目中(具体方法见:[【FastDev4Android框架开发】AndroidAnnnotations注入框架介绍和Android Studios基本配置(七)](http://blog.csdn.net/developer_jiangqq/article/details/49468923) * 集成RoboGuice框架到项目中(具体方法见:[RoboGuice使用详解](http://blog.csdn.net/column/details/devroboguice.html)) * 修改我们的Activity,现在不需要该Activity继承RoboActivity了,只要对该Activity使用@RoboGuice注解即可。(原来使用RoboGuice是需要Activity继承RoboActivity的)。 * 现在让我们来看一下使用的例子 ~~~ @EActivity(R.layout.main) @RoboGuice({AstroListener.class,AnotherListener.class}) public classAstroGirl extends Activity { @ViewById EditText edit; @Inject GreetingService greetingService; @Click void button() { String name =edit.getText().toString(); greetingService.greet(name); } } public classAstroListener { @Inject Context context; public voiddoSomethingOnResume(@Observes OnResumeEvent onResume) { Toast.makeText(context,"Activity has been resumed", Toast.LENGTH_LONG).show(); } } ~~~ 2.3.RoboGuice优点 RoboGuice对于Android应用程序有GoogleGuice框架的支持,这意味着我们可以从依赖注入获得更加的优势,高内聚,低耦合。 2.4.AndroidAnnotations优点 * 在Android编译的时候进行注入组件(没有性能的影响) * 我们不需要在继承RoboActivity类了,这意味中我们可以集成任何类了,哪怕这些类不支持RoboGuice。这样我们就可以更多思考如何扩展我们的基类。 * 支持@Click,@Background,@UiThread,@ItemSelect等等注解。 2.5.@EBean中使用RoboGuice 自AndroidAnnotations2.4起 假设我们现在有一个@EBean注解的组件,现在我们想用RoboGuice来注入一些组件。使用@EActivity注解的Activity允许使用RoboGuice注入。不过现在RoboGuice现在仅支持Activity。从AndroidAnnotations官方发布的消息是:现在他们没有花很多精力在RoboGuice上面,因为如果要全面支持RoboGuice,那么现在项目需要改动很多地方了。 但是这也不意味着我们不能在@EBean注解的类中使用RoboGuice,你只需要手动进行Guice注入,就和RoboActivity达到一样的效果啦。使用实例如下: ~~~ @EBean public class MyBean{ @App MyRoboApplication application; @Inject SomeClass myRoboGuiceDependency; @AfterInject void injectRoboGuiceDependencies() { application.getInjector().injectMembers(this); } } ~~~ 到此位置关于AndroidAnnotations第三方框架集成之RoboGuice集成已经全部讲解完成了。 同时FastDev4Android项目已经添加配置了AndroidAnnotations框架,后期的框架项目中也会主要使用这个DI框架,.欢迎大家去Github站点进行clone或者下载浏览:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android) 同时欢迎大家star和fork整个开源快速开发框架项目~