Android RoboGuice2 Use Guide (4) Comprehensive example Astroboy

Source: Internet
Author: User
Tags requires

The basic usage of RogoGuice2.0 is described earlier, and other uses can be found in the RoboGuice1.1 Development Guide, which provides support for Fragment,view (in custom View use injection), which is not covered in this blog.

This example uses the simple example Astroboy (Astro) in the Roboguice development package. Some common methods involved in using RoboGuice2.0.

This example downloads (Eclipse project).

The following is an explanation of the use of RoboGuice2 in the project. Because this example does not use a custom binding, you do not need to define module with Res/values/roboguice.xml. If you have a custom module, see the Android RoboGuice2 Guide (2): The first example is Hello world.

1. Class Astroboy

There ' s only one astroboy, so make it a @Singleton.     
This means that there would be only one instance of the Astroboy in the entire//app.     
Any class This requires an instance of Astroboy'll get the same instance. This is also means this class needs to is thread safe, of course @Singleton public class Astroboy {//Bec Ause Astroboy is a Singleton, we can ' t directly inject the current//context since the Nge depending on what//are 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'll 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.maketex     
    T (application, "Astroboy says, \" "+ Something +" \ ", Toast.length_long). Show (); public void Brushteeth () {vibrator.vibrate (new long[] {0, 200, 50, 200, 50, 20     
                0, 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)]; }     
}

You only want to use a Astroboy instance in your program, so you can use the @singleton annotation, and any subsequent use of the

@Inject Astroboy Astroboy;

The injected Astroboy will point to the same instance, which is consistent with the singleton design pattern.

@Inject application Application; Inject the application instance. See Android Roboguice Usage Guide: Inject context

@Inject Vibrator Vibrator; Inject Android Vibrator example, see Android Roboguice Usage Guide: Standard injection

@Inject Random Random; For a normal Java type (POJO), you can also use Roboguice to automatically inject an instance if the class has a default constructor (without arguments, and so on).

So when Astroboy is created, Roboguice automatically application, vibrator, random create instances without using new or parameter passing in to construct them.

2. Class Astroboyremotecontrol

/** * A class to control astroboy remotely.    
 * This class uses the "current context" so we must make it @ContextSingleton. * This means that there would be one astroboyremotecontrol for every activity or * service that requires one. This is the "we actually ask for" activity, rather * than "the context" (which is the same thing), because we need AC     
 Cess to some * activity-related methods and this saves the US from have to downcast to a * 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 while to use it *-I to inject a activit 

Y instead of a context (which are 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 W ITH @SiNgleton, so this instance//of Astroboy would be the same instance used elsewhere in our app. Injecting an the basically equivalent to "@Inject context",//and thus also requires @Conte Xtscope.     
    If you are wanted, you could also//@Inject application, Service, etc. wherever appropriate.     
    @Inject Astroboy Astroboy;     
          
    activity of @Inject activity; public void Brushteeth () {//More info about logging available://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, "Your EVil remote control has exploded!     
        Now Astroboy are freeeeeeeeee! ", Toast.length_long). Show ();     
    Activity.finish (); }     
}

A scope annotation similar to singleton is @contextsingleton, which indicates that there is an instance for each activity instance, and that different activity corresponds to a different instance.

@Inject Astroboy Astroboy; Inject the same Astroboy instance (Singleton).

@Inject Astroboy Astroboy; Inject the corresponding activity instance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.