Android 的RoboGuice介绍

RoboGuice 使得进行Android开发更加方便,使得开发变得更加简单也更有乐趣。当你调用getIntent().getExtras()是不是经常检查是否为null?RoboGuice可以帮助你。想想调用findViewById()并映射到TextView确实有必要么?RoboGuice也可以帮你。

RoboGuice 可以帮助解决这类的判断工作。你再也不用记住,是通过调用bindService获取一个用户service,调用getSystemService获取一个系统service。注入你的view、service、或者其他对象,然后让RoboGuice来处理剩下的事宜。

RoboGuice 精简了你的应用代码。更少的代码意味着bug也会更少。也使得阅读代码更加容易,不在纠缠于Android平台的各种特性,而是关注于应用实际的业务逻辑。

没什么很难的,你所需要做的仅仅是配置RoboGuice。

http://code.google.com/p/roboguice/

RoboGuice最近推出了2.0版本。和1.1相比具有:

提高了稳定性
支持Fragment
更简洁易用
但由于RoboGuice2 不完全向下兼容RoboGuice1.1,因此原来使用RobuGuice1.1开发的项目需要对代码做些修改才可以使用RoboGuice2.0.

这里主要说明一下使用Eclipse IDE开发环境升级到RoboGuice2.0 的一些升级注意事项:

1. 下载新的RoboGuice库,Roboguice2.0 库有四个库组成,如下图所示:

 

\

下载
2. 原先1.1中的RoboApplication 在2.0 已经不存在了。2.0使用上更方便,通常的应用如果不是有自定义绑定的话,无需再派生Application。

3. 如果你使用了自定义的Module来定义Bindings,在2.0中可以通过XML来定义,比如在res/values/roboguice.xml 定义

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<string-array name=”roboguice_modules”>
<item>com.pstreets.guice.demo.GreetingModule</item>
</string-array> </resources>

4. 原先由AbstractAndroidModule派生的类,在2.0中改为AbstractModule ,如:

5. 修改AndroidManifest.xml ,去除原来定义的RoboApplication派生类定义,并在代码中去除RoboApplication派生类定义。

下载新的RoboGuice库,Roboguice2.0 库有四个库组成,如下图所示:\
2. 创建一个新Android项目,比如GuiceDemo,目标平台Android1.5以上。

3. 一般可以在该项目下添加一个libs目录,将两个jar文件拷到libs目录下,然后通过: Project > Properties > Java Build Path > Libraries > Add JARs

 \

注:从ADT17开始,添加的jar文件需放在libs 子目录下,可以参见升级到ADT 17 出现dalvikvm: Unable to resolve superclass的问题

添加了对应guice 和roboguice库的引用之后,就可以开始编写第一个使用roboguice2 的例子。

使用roboguice2 的步骤:

Roboguice2 中不在含有RoboApplication 类,因此无需也不可能派生RoboApplication的子类。这里重复一下HelloWorld 的Layout 和类说明

1. 在这个简单的例子中,它使用的Layout 定义如下:

<?xml version=”1.0″ encoding=”utf-8″?>
< LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:id=”@+id/hello”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
< /LinearLayout>
我们定义了一个TextView ,它的id为hello.

假定这个应用使用一个IGreetingService ,它有一个方法getGreeting() 返回一个字符串,至于IGreetingService 如何实现,GuideDemo 不需要关心。

 \

Dependency injection 设计模式的一个核心原则为: Separate behavior from dependency resolution. 也就说将应用需要实现的功能和其所依赖的服务或其它对象分离。 对本例来说GuiceDemo只要知道它依赖于IGreetingService 服务,至于IGreetingService有谁实现GuiceDemo并不需要知道。

在Roboguice 中使用@Inject 来表示这种依赖关系。

[java] 
public class GuiceDemo extends RoboActivity  { 
  
 @InjectView (R.id.hello) TextView helloLabel; 
 @Inject IGreetingService greetingServce; 
  
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 helloLabel.setText(greetingServce.getGreetings()); 
 } 

public class GuiceDemo extends RoboActivity  {
 
 @InjectView (R.id.hello) TextView helloLabel;
 @Inject IGreetingService greetingServce;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 helloLabel.setText(greetingServce.getGreetings());
 }
}
使用RoboGuice 的Activity需要从RoboActivity派生(RoboActivity为Activity的子类).
使用@Inject标注greetingServce依赖于IGreetingService服务
使用@InjectView表示helloLabel 依赖于R.id.hello (XML)
代码中没有创建greetingServce 对象的代码(如 new xxx()) 和为helloLabel 赋值的代码。这些值都可以Roboguice 自动创建和赋值注入(Inject)到变量中。

为了说明问题,我们在代码中添加两个对getGreetings的实现,一个为HelloWorld, 一个为HelloChina:

[java] 
public class HelloChina implements IGreetingService{ 
  
 @Override 
 public String getGreetings() { 
 return "Hello,China"; 
 } 
  

  
public class HelloWorld implements IGreetingService{ 
  
 @Override 
 public String getGreetings() { 
 return "Hello,World"; 
 }  

public class HelloChina implements IGreetingService{
 
 @Override
 public String getGreetings() {
 return "Hello,China";
 }
 
}
 
public class HelloWorld implements IGreetingService{
 
 @Override
 public String getGreetings() {
 return "Hello,World";
 }
 
}
2. 到这里,你可能有些困惑,RoboGuice怎么知道使用那个类(HelloWorld或是HelloChina)为GuiceDemo中的greetingServce 赋值呢?这是通过在Module 中定义binding 来实现的。

在项目中添加一个GreetingModule (从AbstractModule 派生而非AbstractAndroidModule类)重载configure方法:

[java] 
public class GreetingModule extends AbstractAndroidModule{ 
  
@Override 
protected void configure() { 
bind(IGreetingService.class).to(HelloWorld.class); 
//bind(IGreetingService.class).to(HelloChina.class);  
  
}   

public class GreetingModule extends AbstractAndroidModule{
 
@Override
protected void configure() {
bind(IGreetingService.class).to(HelloWorld.class);
//bind(IGreetingService.class).to(HelloChina.class);
 
}
 
}
将IGreetingService 绑定到HelloWorld 类。

3. 在res/values/roboguice.xml 定义Module

[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="roboguice_modules"> 
        <item>com.pstreets.guice.demo.GreetingModule</item> 
    </string-array> 
</resources> 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="roboguice_modules">
        <item>com.pstreets.guice.demo.GreetingModule</item>
    </string-array>
</resources>
可以将GreetingModule 绑定改为HelloChina ,对比一下:

 

\

通过改变binding ,GuiceDemo 显示了不同的结果,GuiceDemo不依赖于具体的实现,可以非常方便的改变接口的实现而无需更改GuiceDemo的代码。大大降低了类于类之间的耦合性

转载于:https://my.oschina.net/xiahuawuyu/blog/81613

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值