Android roboguice notes

Source: Internet
Author: User

Roboguice 2.0.jar has three dependent packages: guice-3.0-no_aop.jar, javax. inject-1.jar, jsr305-1.3.9.jar
Roboguice 1. *. Jar has a dependency package: guice-2.0-no_aop.jar

2.0 to 1. * advantages:
Improved Stability
Fragment supported
Simpler and easier to use

The difference between 2.0 and 1. * is:

2.0 does not overwrite the application. The roboapplication contained in 2.0 does not exist.

The module binding of 2.0 must inherit abstractmodules. The abstractandroidmodules in the original 1. * does not exist.

2.0 adds support for fragment. Therefore, when developing Android 2.0 or later versions, can better support this attribute.

When developing applications, there is a principle of modularization, while the coupling between modules should be minimized,
A typical mode for reducing Coupling Based on frameworks is dependency injection. In the Spring framework, Google's guice and Google's Android-based roboguice framework,
All have the dependency injection function. With the framework dependency injection, we do not need to use constructors or factory methods to instantiate objects. Instead, the framework itself describes the relationship between classes, automatic injection. The essence of spring's reverse control is bottom-up dependency injection.
Of course, guice and roboguice use some factory methods in some places,ProgramIt does not rely on the workshop method to create objects, but only supports the APIS provided by the framework for external use,

 

Roboguice is a framework designed by guice Based on the Android platform, with less complicated searchCodeAnd Object Instantiation. The following describes the features of roboguice in 11 steps:

1. supported when a class has a default constructor
@ Inject classname demo;

2. Android resource injection is supported:
@ Injectview (R. Id. XXX) textview name;
@ Injectresource (R. drawable. Icon) drawable icon;

3. Supports android standard API injection:

@ Inject contentresolver;
@ Inject assetmanager;
@ Inject resources Resources;
@ Inject locationmanager;
@ Inject windowmanager;
@ Inject layoutinflater;
@ Inject activitymanager;
@ Inject powermanager;
@ Inject alarmmanager;
@ Inject icationicationmanager notificationmanager;
@ Inject keyguardmanager;
@ Inject searchmanager;
@ Inject vibrator;
@ Inject connectivitymanager;
@ Inject wifimanager;
@ Inject inputmethodmanager;
@ Inject sensormanager;
@ Inject Application;
@ Inject context;

4. bind multiple implementations:

For example:
Interface:

Public interface phoneservice {
Public void call ();
}

Implementation:
Implementation 1:
Public class phonestyleone implements phoneservice {
Public phonestyleone (){
System. Out. println ("phonestyleone ");
}

@ Override
Public void call (){
System. Out. println ("phonestyleone: phonestyleone ");
}
}

Implementation 2:
Public class phonestyletwo implements phoneservice {
Public phonestyletwo (){
System. Out. println ("phonestyletwo ");
}

@ Override
Public void call (){
System. Out. println ("phonestyletwo: phonestyletwo ");
}

}

In this case, there are two binding methods:

First: Custom annotation,

Definition annotation:

@ Bindingannotation
@ Target ({elementtype. Field, elementtype. parameter, elementtype. Method })
@ Retention (retentionpolicy. runtime)
Public @ interface Po {
//....
}

@ Bindingannotation
@ Target ({elementtype. Field, elementtype. parameter, elementtype. Method })
@ Retention (retentionpolicy. runtime)
Public @ interface PW {
//....
}

Binding:
BIND (phoneservice. Class). annotatedwith (PO. Class). To (phonestyleone. Class );
BIND (phoneservice. Class). annotatedwith (PW. Class). To (phonestyletwo. Class );

Injection:
@ Inject @ Po phoneservice PS1;
@ Inject @ PW phoneservice PS2;

Type 2: Mark with name Annotation

Binding:
BIND (phoneservice. Class). annotatedwith (names. Named ("phone"). To (phonestyleone. Class );
BIND (phoneservice. Class). annotatedwith (names. Named ("phtwo"). To (phonestyletwo. Class );
Injection:
@ Inject @ named ("phone") phoneservice PS1;
@ Inject @ named ("phtwo") phoneservice PS2;

5. instance binding:

Instance binding. Generally, some basic types and string types are bound. We do not recommend Binding Complex types to specific instances.
BIND (integer. Class). annotatedwith (names. Named ("width"). toinstance (10 );
BIND (string. Class). annotatedwith (names. Named ("name"). toinstance ("zhangsan ");

6. Binding Complex types of instances:

Because roboguice does not officially recommend using instance to bind complex types, the correct method should be to use @ providers,
Definition:
In the abstractmodule instance, define the method and use @ provides for annotation:
@ Provides @ named ("myphoneservice ")
Phoneservice providemyphoneservice (){
Return new phoneservice (){
@ Override
Public void call (){
System. Out. println ("providers phoneservice ");
}
};
}

@ Provides @ Po
Phoneservice providemyphoneservice1 (){
Return new phoneservice (){
@ Override
Public void call (){
System. Out. println ("providers phoneservice one ");
}
};
}
Injection:
@ Inject @ named ("myphoneservice") phoneservice myservice;
@ Inject @ Po phoneservice ms1;

You can also define the provider to implement a separate class. This class must implement the roboguice interface:

Define provider:
Public class providerstring implements provider <phonestyletwo> {

@ Override
Public phonestyletwo get (){
System. Out. println ("Get phonestyletwo ");
Return new phonestyletwo ();
}

}
Binding:
BIND (phonestyletwo. Class). toprovider (providerstring. Class );
Injection:
@ Inject phonestyletwo two;

7. Singleton:

Roboguice allows reuse of class instances within a certain range. There are two tags that can be used to specify a class as a singleton:
@ Singleton
@ Contextscoped

8. The default is the second implementation method:

@ Implementedby (class)
Public interface interfaceone {
Void method ();
}
And
BIND (interfaceone. Class). To (class); equivalent,
If both @ implementedby (class) and bind exist, the BIND implementation will be used first.

@ Providedby (provider. Class)
Public interface transactionlog {
Void method ();
}
And Bind (transactionlog). toprovider (class );
If both @ providedby (class) and bind exist, the BIND implementation is preferred.

9. Binding of parameterized types:

Use typeliteral to create binding of this type,
BIND (New typeliteral <list <string >>() {}). toinstance (New arraylist <string> ());
Or use @ providers
@ Provides
List <string> providestring ()
{
Return new Response List <string> ();
}

10. annotation summary:

@ Injectextra
@ Injectpreference
@ Injectresouece
@ Injectview
@ Inject
@ Sharedpreferencesname

In addition, roboguice provides simple message publish/subscribe mechanisms, as well as robothread, roboasynctask, and robolooperthread that support dependency injection,

 

11. roboguice's support for various resource files and data transmission:

@ Injectview (R. Id. styled_text) textview styled_text;
@ Injectview (R. Id. plain_text) textview plain_text;
@ Injectview (R. Id. RES1) textview RES1;
@ Inject resources res;
@ Injectresource (R. String. styled_text) string STR;
@ Injectextra ("extra2") string extra2;
@ Injectextra (value = "extra3", optional = true) string extra3;

 

Attach several useful images:

 

This example of exercise is: http://files.cnblogs.com/Gordon-YangYiBao/roboguice.rar

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.