在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 第一個例子 Hello World
Just-in-time Bindings
Binding Annotations
Standard Injection
第一個例子 Hello World
Bindings 概述
Linked Bindings
如何綁定 generic 類型
@Provides Methods
RoboGuice 功能描述
概述
綜合示例 Astroboy
Inject Resources
Instance Bindings
Inject 自定義 View
Scopes
Provider Bindings
Untargetted Bindings
Inject Extra
第一個例子 Hello World
Inject Context
發(fā)送接收 Events
Inject View

第一個例子 Hello World

本例實現(xiàn)的功能和例子 Android RoboGuice 使用指南(2):第一個例子Hello World 一樣,所不同的是本例使用 RoboGuice2.0 來實現(xiàn)。

  1. 下載新的 RoboGuice 庫,Roboguice2.0 庫有四個庫組成,如下圖所示:

http://wiki.jikexueyuan.com/project/android-roboguice/images/22.png" alt="" />

庫可以從 http://code.google.com/p/roboguice/ 下載,也可以從本站 下載

  1. 創(chuàng)建一個新 Android 項目,比如 GuiceDemo,目標平臺 Android1.5 以上。

  2. 一般可以在該項目下添加一個 libs 目錄,將兩個 jar 文件拷到 libs 目錄下,然后通過: Project > Properties > Java Build Path > Libraries > Add JARs

http://wiki.jikexueyuan.com/project/android-roboguice/images/23.png" alt="" />

注:從 ADT17 開始,添加的 jar 文件需放在 libs 子目錄下,可以參見升級到 ADT 17 出現(xiàn) dalvikvm: Unable to resolve superclass 的問題

添加了對應(yīng) guice 和 roboguice 庫的引用之后,就可以開始編寫第一個使用 roboguice2 的例子。

使用 roboguice2 的步驟:

Roboguice2 中不在含有 RoboApplication 類,因此無需也不可能派生 RoboApplication 的子類。這里重復(fù)一下 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.

假定這個應(yīng)用使用一個 IGreetingService ,它有一個方法 getGreeting() 返回一個字符串,至于 IGreetingService 如何實現(xiàn),GuideDemo 不需要關(guān)心。

http://wiki.jikexueyuan.com/project/android-roboguice/images/24.png" alt="" />

Dependency injection 設(shè)計模式的一個核心原則為: Separate behavior from dependency resolution. 也就說將應(yīng)用需要實現(xiàn)的功能和其所依賴的服務(wù)或其它對象分離。 對本例來說 GuiceDemo 只要知道它依賴于 IGreetingService 服務(wù),至于 IGreetingService 有誰實現(xiàn)GuiceDemo 并不需要知道。

在 Roboguice 中使用 @Inject 來表示這種依賴關(guān)系。

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 服務(wù)
  • 使用 @InjectView 表示 helloLabel 依賴于 R.id.hello (XML)

代碼中沒有創(chuàng)建 greetingServce 對象的代碼(如 new xxx()) 和為 helloLabel 賦值的代碼。這些值都可以 Roboguice 自動創(chuàng)建和賦值注入(Inject) 到變量中。

為了說明問題,我們在代碼中添加兩個對 getGreetings 的實現(xiàn),一個為HelloWorld, 一個為 HelloChina:

public class HelloChina implements IGreetingService{

 @Override
 public String getGreetings() {
 return "Hello,China";
 }

}

public class HelloWorld implements IGreetingService{

 @Override
 public String getGreetings() {
 return "Hello,World";
 }

}
  1. 到這里,你可能有些困惑,RoboGuice 怎么知道使用那個類(HelloWorld 或是 HelloChina)為 GuiceDemo 中的 greetingServce 賦值呢?這是通過在 Module 中定義 binding 來實現(xiàn)的。

在項目中添加一個 GreetingModule (從 AbstractModule 派生而非 AbstractAndroidModule 類)重載 configure 方法:

public class GreetingModule extends AbstractAndroidModule{

@Override
protected void configure() {
bind(IGreetingService.class).to(HelloWorld.class);
//bind(IGreetingService.class).to(HelloChina.class);

}

}

將 IGreetingService 綁定到 HelloWorld 類。

  1. 在 res/values/roboguice.xml 定義 Module
<?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 ,對比一下:

http://wiki.jikexueyuan.com/project/android-roboguice/images/25.png" alt="" />

通過改變 binding ,GuiceDemo 顯示了不同的結(jié)果,GuiceDemo 不依賴于具體的實現(xiàn),可以非常方便的改變接口的實現(xiàn)而無需更改 GuiceDemo 的代碼。大大降低了類于類之間的耦合性。