Android Roboguice Open Source framework uses

Source: Internet
Author: User

Android Roboguice App

Open source Roboguice is a dependency injection framework, and if you have used spring you should know its benefits.
By reducing the burden on programmers, the code becomes more concise.

Address: Https://github.com/roboguice/roboguice

The tool uses Android Studio because Gradle can automatically add third-party libraries.

Gradle part of the content:

dependencies {    ‘libs‘, include: [‘*.jar‘])    ‘com.android.support:appcompat-v7:22.1.1‘    ‘org.roboguice:roboguice:2.0‘    compile (‘com.google.inject.extensions:guice-assistedinject:3.0‘){        group‘com.google.inject‘    }}

Version with 2.0, the official version of the latest is 3.0

Demonstrate basic usage

ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.TextView;ImportCom.google.inject.Binder;ImportCom.google.inject.ImplementedBy;ImportCom.google.inject.Inject;ImportCom.google.inject.Module;Importroboguice.activity.RoboActivity;ImportRoboguice.inject.ContentView;ImportRoboguice.inject.InjectView;//The Setcontentview method is called@ContentView(R.layout.activity_main) Public  class mainactivity extends roboactivity {    @InjectView(R.ID.TEXT1)PrivateTextView TextView;//Instead of Findviewbyid do not own a strong turn type    @InjectView(R.ID.BTN1)PrivateTextView button;@Inject    PrivateMycache<string,string> Mycache;//single row    @Inject    PrivateIService MyService;//Interface injection    @Inject    PrivateToaster Toaster;//toast Simple usage Encapsulation    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Button.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {Textview.settext ("Test is OK."+ Mycache); Toaster.showmsg ("Hey, I like the talk to You"); Myservice.login ("1","1");    }        }); }}

Toaster.java

Importandroid.app.Activity;ImportAndroid.widget.Toast;ImportCom.google.inject.Inject;ImportRoboguice.inject.ContextSingleton;/** * Created by Gaofeng on 15-5-13. * *//Can get the context in the current activity@ContextSingleton Public  class Toaster {    @Inject    PrivateActivity activity;//Do not pass the context     Public void showmsg(String msg) {//Do not pass in context,activity context object yourselfToast.maketext (Activity,msg, Toast.length_short). Show (); }}mycache.javaImportCom.google.inject.Singleton;ImportJava.util.HashMap;ImportJava.util.Map;/** * Created by Gaofeng on 15-5-13. * *//cache are usually single-column@Singleton Public  class Mycache<K,V> { //Here is just demo, no writing too complicatedmap<k,v> map =NewHashmap<k,v> (); Public void put(K k,v T)    {map.put (k,t); } PublicVGet(k K) {returnMap.get (k); }}

The following is the interface instantiation of the code, do Java SSH Web development is generally like this

 Public  interface iservice { //Simple Business Interface     Public void Login(String u,string passwd);}ImportRoboguice.inject.ContextSingleton; Public  class myservice implementsiservice {   @Inject    PrivateMycache<string,string> Mycache;//And last object is the same, single-column object    PrivateContext context; Public MyService() {    } Public MyService(Context context) { This. Context = Context; }@Override     Public void Login(String u, string passwd) {LOG.D ("","Service login>>>>"+ Context +" "+ Mycache);//Business code XXXX}}

Implementing the above interface implementation requires configuration registration

ImportAndroid.content.Context;ImportAndroid.util.Log;ImportCom.google.inject.AbstractModule;/** * Created by Gaofeng on 15-5-13. * * Public  class CustomModule2 extends abstractmodule {    PrivateContext context;//The system will pass in the context itself     Public CustomModule2(Context context) { This. Context = Context; }@Override    protected void Configure() {LOG.D ("","Custommodule2>>>>>>>"); Bind (Iservice.class). ToInstance (NewMyService (context));//Registration}}

You also need to configure the Add files in res/values/ roboguice.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="roboguice_modules">        <item>com.example.gaofeng.roboguice_demo.CustomModule2</item>    </string-array></resources>

Mastering basic usage can reduce a lot of code.
No need to go to the new object, a single row is also very good, do not define the static object

Reference: Https://github.com/roboguice/roboguice/wiki

Android Roboguice Open Source framework uses

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.