当前位置:操作系统 > 安卓/Android >>

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

1. 类Astroboy

[java]
// 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)]; 
    } 

// 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

[java] 
/**
 * 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 basicall

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,