Android roboguice open-source framework

Source: Internet
Author: User

Android roboguice open-source framework
Android roboguice Application

Open-source roboguice is a dependency injection framework. If you have used Spring, you should know its advantages.
Reduce the burden on programmers and simplify the code.

 

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

Gradle:

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

The version is 2.0, and the official version is 3.0.

Demonstrate basic usage

Import android. OS. bundle; import android. view. view; import android. widget. textView; import com. google. inject. binder; import com. google. inject. implementedBy; import com. google. inject. inject; import com. google. inject. module; import roboguice. activity. roboActivity; import roboguice. inject. contentView; import roboguice. inject. injectView; // call the setContentView method @ ContentView (R. layout. activity_main) public class MainActivity extends RoboActivity {@ InjectView (R. id. text1) private TextView textView; // replace findViewById. You do not need to convert the type by yourself @ InjectView (R. id. btn1) private TextView button; @ Inject private MyCache
  
   
MyCache; // Single Column @ Inject private IService myService; // interface injection @ Inject private Toaster toaster; // encapsulation of simple Toast usage @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {textView. setText (Test is OK. + myCache); toaster. showMsg (hey, I like to talk to you); myService. login );}});}}
  

Toaster. java

Import android. app. activity; import android. widget. toast; import com. google. inject. inject; import roboguice. inject. contextSingleton;/*** Created by gaofeng on 15-5-13. * // you can obtain the context @ ContextSingletonpublic class Toaster {@ Inject private Activity in the current Activity. // you do not need to transmit the context public void showMsg (String msg) {// you do not need to input Context, Activity Context object Toast. makeText (activity, msg, Toast. LENGTH_SHORT ). show () ;}} MyCache. javaimport com. google. inject. singleton; import java. util. hashMap; import java. util. map;/*** Created by gaofeng on 15-5-13. * /// Cache is usually a single column @ Singletonpublic class MyCache
  
   
{// Here is only a demonstration, not writing too complex Map
   
    
Map = new HashMap
    
     
(); Public void put (K k, V t) {map. put (k, t);} public V get (K k) {return map. get (k );}}
    
   
  

The following is the code for interface instantiation. This is generally the case for java ssh web development.

Public interface IService {// simple business interface public void login (String u, String passwd);} import roboguice. inject. ContextSingleton; public class MyService implements IService {@ Inject private MyCache
  
   
MyCache; // It is the same as the previous object, with a single column object private Context 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 }}
  

To implement the real-name listing of the above interfaces, You need to configure registration.

Import android. content. context; import android. util. log; import com. google. inject. abstractModule;/*** Created by gaofeng on 15-5-13. */public class CustomModule2 extends actmodule {private Context context; // The system will pass in the Context public CustomModule2 (context Context) {this. context = context ;}@ Override protected void configure () {Log. d (, CustomModule2 >>>>>>); bind (IService. class ). toInstance (new MyService (context); // register }}

You also need to add files in res/values /.Roboguice. xml


  
      
           
    
     com.example.gaofeng.roboguice_demo.CustomModule2
        
   
  

Understanding the basic usage can reduce a lot of code.
You don't have to go to the new object on your own, and the single column is also very easy to get, you don't have to define the static object on your own

 

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.