Android RoboGuice User Guide (2): Example 1 Hello World

Source: Internet
Author: User

First, we will introduce how to add the Guice and RoboGuice libraries to the project.
1. Download RoboGuicehttp: // code.google.com/p/roboguice/wiki/downloadsand guice-2.0-no_aop.jar (not guice-3.0) Labels

2. Create a new Android project, such as GuiceDemo and the Target Platform Android1.5 or above.

 

3. Generally, you can Add a lib directory under the Project, copy the two jar files to the lib directory, and use Project> Properties> Java Build Path> Libraries> Add External JARs


After adding references to the corresponding guice and roboguice libraries, you can write the first example using roboguice.
Steps for using roboguice:
 

1. Create a RoboApplication subclass GuiceApplication. GuiceApplication is a subclass of Appliacation. Therefore, you need to modify AndroidManifest. xml and point the Application name to this class.
<Application android: name = "GuiceApplication"
Android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". GuiceDemo"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
2. In this simple example, Layout is defined as follows:
<? 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>
We have defined a TextView, whose id is hello.

Assume that this application uses an IGreetingService, And the getGreeting () method returns a string. GuideDemo does not need to care about how IGreetingService is implemented.

A core principle of the Dependency injection design mode is: Separate behavior from dependency resolution. This means that the functions to be implemented by the application are separated from the services or other objects on which it depends. For this example, GuiceDemo only needs to know that it depends on the IGreetingService service, but does not need to know who implements GuiceDemo for IGreetingService.
In Roboguice, @ Inject is used to represent this dependency.
 
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 ());
}
}
The Activity that uses RoboGuice needs to be derived from RoboActivity (RoboActivity is a subclass of Activity ).
Use @ Inject to mark greetingServce dependent on IGreetingService
Use @ InjectView to indicate that helloLabel depends on R. id. hello (XML)
The Code does not contain the code for creating a greetingServce object (such as new xxx () and the Code for assigning values to helloLabel. These values can be automatically created by Roboguice and injected into the variable by value assignment.
To illustrate the problem, we add two getGreetings implementations in the Code: HelloWorld and 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 ";
}

}
3. You may be confused here. How does RoboGuice know to use that class (HelloWorld or HelloChina) to assign values to greetingServce in GuiceDemo? This is implemented by defining binding in the Module.
Add a GreetingModule (derived from AbstractAndroidModule) to the project to overload the configure method:
[Java]
Public class GreetingModule extends AbstractAndroidModule {

@ Override
Protected void configure (){
Bind (IGreetingService. class). to (HelloWorld. class );
// Bind (IGreetingService. class). to (HelloChina. class );

}

}
Bind IGreetingService to the HelloWorld class.
Add the preceding modules in addapplicationmodumodules of GuiceApplication:
[Java]
Public class GuiceApplication extends RoboApplication {

Protected void addApplicationModules (List <Module> modules ){
Modules. add (new GreetingModule ());
}

}


You can bind the GreetingModule to HelloChina. The comparison is as follows:

By changing the binding, GuiceDemo shows different results. GuiceDemo does not depend on the specific implementation, so it is very convenient to change the implementation of the interface without changing the GuiceDemo code. This greatly reduces the coupling between classes.
The Binding types and usage (Guice) supported by Guice and RoboGuice and Dependency injection (RoboGuice) related to the android platform will be introduced one by one)
Download this example (including roboguice Library): http://www.bkjia.com/uploadfile/2012/0504/20120504094028861.zip

 


Excerpted from the mobile app

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.