Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android RoboGuice2使用指南(4) 綜合示例Astroboy

Android RoboGuice2使用指南(4) 綜合示例Astroboy

編輯:Android開發教程

前面介紹了RogoGuice2.0的基本用法,其它使用可以參見RoboGuice1.1開發 指南,2.0中提供了對Fragment,View(自定義View中使用注入)的支持,本博 客不再一一介紹。

本例使用的是RoboGuice 開發包中的簡單示例 Astroboy(阿童木)。涉及的使用RoboGuice2.0 的一些常用方法。

本例 下載(Eclipse項目)。

下面對項目中RoboGuice2的使用進行解釋。因為 本例沒使用自定義綁定,所以無需使用res/values/roboguice.xml 定義Module. 如有自定義模塊,可以參見Android RoboGuice2 使用指南(2): 第一個例子 Hello World。

1. 類Astroboy

// There's only one Astroboy, so make it a @Singleton.     
// This means that there will be only one instance of Astroboy in the 

entire     
// app.     
// Any class that requires an instance of Astroboy will get the same 

instance.     
// This also means this class needs to be thread safe, of course     
@Singleton 
public class Astroboy {     

    // Because Astroboy is a Singleton, we can't directly inject the 

current     
    // Context since the current context may change depending on what 

activity     
    // is using Astroboy     
    // at the time. Instead we use the application context.     
    // Vibrator is bound to context.getSystemService(VIBRATOR_SERVICE) 

in     
    // DefaultRoboModule.     
    // Random has no special bindings, so Guice will create a new 

instance for     
    // us.     
    @Inject Application application;     
    @Inject Vibrator vibrator;     
    @Inject Random random;     

    public void say(String something) {     
        // Make a Toast, using the current context as returned by the 

Context     
        // Provider     
        Toast.makeText(application, "Astroboy says, \"" + something + 

"\"",     
                Toast.LENGTH_LONG).show();     
    }

    public void brushTeeth() {     
        vibrator.vibrate(     
                new long[] { 0, 200, 50, 200, 50, 200, 50, 200, 50, 

200, 50,     
                        200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 

200, 50, },     
                -1);     
    }     

    public String punch() {     
        final String expletives[] = new String[] { "POW!", "BANG!", 

"KERPOW!",     
                "OOF!" };     
        return expletives[random.nextInt(expletives.length)];     
    }     
}

程序中只希望使用一個Astroboy實例,因此可以使用@Singleton標注,此後 任何使用

@Inject Astroboy astroboy;

注入的Astroboy都會指向 同一個實例,這也是符合Singleton設計模式的。

@Inject Application application; 注入Application實例。參見Android RoboGuice 使用指南 (15):Inject Context

@Inject Vibrator vibrator; 注入Android Vibrator實例,參見Android RoboGuice 使用指南(16):Standard Injection

@Inject Random random; 對於普通的Java 類型(POJO),如 果該類具有缺省構造函數(不帶參數的等),也可以使用RoboGuice自動注入實 例。

因此當Astroboy創建時,RoboGuice 自動為application, vibrator, random 創建實例,無需使用new 或參數傳入來構造它們。

2. 類AstroboyRemoteControl

/**    
 * A class to control Astroboy remotely.    
 *    
 * This class uses the current context, so we must make it 

@ContextSingleton.    
 * This means that there will be one AstroboyRemoteControl for every 

activity or    
 * service that requires one. Note that we actually ask for the 

Activity, rather    
 * than the Context (which is the same thing), because we need access 

to some    
 * activity-related methods and this saves us from having to downcast 

to an    
 * Activity manually.    
 *    
 * It also asks RoboGuice to inject the Astroboy instance so we can 

control him.    
 *    
 * What you'll learn in this class - What @ContextScope means and when 

to use it    
 * - How to inject an Activity instead of a Context (which is really 

the same    
 * thing) - How to use RoboGuice's convenient and flexible logging 

facility, Ln.    
 */ 
@ContextSingleton 
public class AstroboyRemoteControl {     
          
    // The Astroboy class has been decorated with @Singleton, so this 

instance     
    // of Astroboy will be the same instance used elsewhere in our 

app.     
    // Injecting an Activity is basically equivalent to "@Inject 

Context context",     
    // and thus also requires @ContextScope. If you wanted, you could 

also     
    // @Inject Application, Service, etc. wherever appropriate.     
    @Inject Astroboy astroboy;     
    @Inject Activity activity;     
          
    public void brushTeeth() {     
        // More info about logging available here:     
        // http://code.google.com/p/roboguice/wiki/Logging     
        Ln.d("Sent brushTeeth command to Astroboy");     
        astroboy.brushTeeth();     
    }     
          
    public void say(String something) {     
        Ln.d("Sent say(%s) command to Astroboy", something);     
        astroboy.say(something);     
    }     
          
    public void selfDestruct() {     
        Toast.makeText(     
                activity,     
                "Your evil remote control has exploded! Now Astroboy 

is FREEEEEEEEEE!",     
                Toast.LENGTH_LONG).show();     
        activity.finish();     
    }     
}

與Singleton類似的一個Scope標注為@ContextSingleton ,它表示對 於每個Activity實例有一個實例,不同的activity對應不同的實例。

@Inject Astroboy astroboy; 注入同一個Astroboy實例(Singleton) 。

@Inject Astroboy astroboy; 注入對應的Activity實例。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved