[转]RoboGuice介绍

一些常用和普遍的依赖注入库有:



 

RoboGuice:



 

Roboguice是一个用于Android应用的依赖注入框架,使用Google官方的Guice库位极大地简化了Android的依赖注入。让你的Android应用开发之路更加平坦顺利,编程更加简单有趣。

当你调用 getIntent(),getExtras()这些函数时你是否经常忘记检查是否为Null?RoboGuice可以帮助你。初始化TextView有必要调用findViewById()吗?不用,RoboGuice已经为你完成了。

通过使用RoboGuice,你可以注入View视图控件、资源、系统服务或者其他任何对象。RoboGuice能帮你精简应用程序的代码。代码越少意味着出现问题或bug的次数也就越少,从而可以把更多的精力花在项目中那些需要编写或修改的部分,使得阅读代码更加容易。

让我们来看看各种RoboGuice 库的使用方法。

使用RoboGuice库 :

  • 控件注入:用@InjectViews方法初始化控件,例如:@InjectView(R.id.textview1)TextView textView1。
  • 资源注入:用@InjectResources方法初始化资源,例如:@InjectResource(R.string.app_name)String name。
  • 系统服务注入:用@Inject方法初始化并获取系统服务,例如:@Inject LayoutInflater inflater。
  • POJO对象注入:用@Inject方法注入并初始化POJO对象,例如:@Inject Foo foo。

安装

要使用RoboGuice,你需要下载JAR文件并把他们添加到环境变量中:

我们来看看一个简单的一般事件代码:

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class TestActivity extends Activity{
 
     TextView textView1;
     TextView textView2;
     ImageView imageView1;
     String name;
     Drawable icLauncher;
     LocationManager locManager;
     LayoutInflater inflater;
     NotificationManager notifyManager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super .onCreate(savedInstanceState);
         setContentView(R.layout.layout_test);
         textView1 = (TextView) findViewById(R.id.textView1);
         textView2 = (TextView) findViewById(R.id.textView2);
         imageView1 = (ImageView) findViewById(R.id.imageView1);
         name = getString(R.string.app_name);
         icLauncher = getResources().getDrawable(R.id.ic_launcher);
         locManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
         inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
         notifyManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
         textView1.setText( "Hello World! RoboGuice demo" );
      }
}

再看看使用RoboGuice精简代码后神奇之处。

使用RoboGuice

你先要继承RoboActivity或者RoboFragment,才能使用RoboGuice的依赖注入功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class TestActivity extends RoboActivity{
 
     @InjectView (R.id.textView1) TextView textView1;
     @InjectView (R.id.textView2) TextView textView2;
     @InjectView (R.id.imageView1) ImageView imageView1;
     @InjectResource (R.string.app_name) String name;
     @InjectResource (R.drawable.ic_launcher) Drawable icLauncher;
     @Inject LocationManager locManager;
     @Inject LayoutInflater inflater;
     @Inject NotificationManager notifyManager;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super .onCreate(savedInstanceState);
         setContentView(R.layout.layout_test);
         textView1.setText(name);
     }
}

这么一对比,我想你肯定明白了为什么要使用RoboGuice?再来看看有哪些好处:

使用RoboGuice的好处

  • 不需要初始化控件,如有需要就用@InjectViews。
  • 不需要初始化系统服务,如有需要就用@Inject。
  • 不需要初始化像Drawable,string以及其他的资源,如有需要就用@InjectResource。
  • 以上实践能帮助你精简代码。
  • 越少的代码,越少的问题和bugs。
  • 少量的代码让Android开发人员省力同时,也让他们能更专注于实际的业务逻辑。

RoboGuice和ActionBarSherlock

正如我前面提到的,你得在RoboActivity和RoboFragment中继承其中一个才能在Activity事件或Fragment中使用RoboGuice。但是如果你已经在项目中使用了ActionBarSherlock去编译呢?那问题就在于,你已经继承了SherlockActivity或SherlockFragmentActivity中的一个。现在问题是,你不能同时使用RoboGuice和ActionBarSherlock。

解决方法是,为Activities和Fragments定义一个基类。然后你就能同时使用RoboGuice和ActionBarSherlock了。

你可以在这里下载一些基类:
https://github.com/rtyley/roboguice-sherlock 或者下载JAR包也是一样:RoboGuice+Sherlock.jar,你可以任选一个添加到你的项目。

在Android应用程序中,我想我已经做了所有关于RoboGuice用法及好处的研究。如过有什么遗漏,请联系我。在接下来的文章,我会研究其他的函数库好让你成为一个既能偷懒又高效的Android开发人员。

猜你喜欢

转载自ayk-tao.iteye.com/blog/2053074
今日推荐