Introduction to the

AndroidAnnotations are an open source framework that allows users to greatly reduce the amount of code they use to create simple code. Official documentation _ Github link

Third-party Library Import

The latest version is 4.1.0

Add the following configuration to build.gradle (local gradle) in app/ :

applyplugin:'com.android.application'
// Add the following two lines of configuration
applyplugin:'android-apt'
defAAVersion='4.0.0'

android{
compileSdkVersion23
buildToolsVersion"23.0.2"

dependencies{
compilefileTree(dir:'libs', include:'*.jar'])
testCompile'junit: junit: 4.12'
compile'com. Android. Support: appcompat - v7:23.1.1'

// Add the two lines of configuration
apt"Org. Androidannotations: androidannotations: $AAVersion"
compile "Org. Androidannotations: androidannotations - API: $AAVersion"
}

// Add the following configuration
apt{
arguments{
androidManifestFilevariant.outputs[0].processResources.manifestFile
resourcePackageName"com.xxx.demo"(Package name of your project)}}Copy the code

The project package name can be confirmed by the package in androidmanifest.xml.

Add the following configuration to the build.gradle file (global gradle) under gradle/ :

dependencies{
classpath'com. Android. Tools. Build: gradle: 1.5.0'
// Add the following line
classpath'com. Neenbedankt. Gradle. Plugins: android - apt: 1.4 +'
}Copy the code

Commonly used annotations

The component annotation

@EActivity(R.layout.acitvity_main)
public class MainActivity extends Activity{... }Copy the code

Common annotations include @eActivity, @eFragment, and @eservice. Only annotated components can use other annotation functions.

Annotations for resource references

 @ViewById(R.i which v_title)// The parentheses can be removed here
 TextView tv_title;

 @ViewById
 ImageView img_menu;

 @ViewById
 RelativeLayout rl_light;

 @Extra
 String mTitle;

 @StringRes(R.string.hello)
 String hello;Copy the code

Simple control binding, the resource file id is the same as the control name, not annotation after parentheses and the corresponding control ID, @extra is the same. The same applies to control iD declarations elsewhere. After the View member variables are initialized, a method with an @AfterViews annotation is called to initialize interface controls.

event

 @Click
    void img_back(a) {     
        finish();
        overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
    }Copy the code

@textChange, @ItemClick, @SeekBarProgresschange, etc.

Some handy notes

Asynchronous threads versus UI threads

@UiThread
void doSomething(a){... }@Background
void doSomething(a){... }Copy the code

UI thread methods with @uithRead and asynchronous thread methods with @background interact by calling each other directly instead of using handlers to send and receive messages.

Radio reception

 @Receiver(actions = Utils.ACTION_BLE_DISCONNETED)
    public void bleDisconnect(a) {... }@Receiver(actions = Utils.ACTION_UPDATE_WATER_SHOW)
 public void updateWaterShow(@Receiver.Extra(Utils.VALUE_ADDRESS) long water) {
     if (switchIsOpen)
         edt_water.setText(water + "");
 }Copy the code

Register for broadcast reception, easy to fix, no other operations required. Compare this to the traditional way:

Private final BroadcastReceiver mGattUpdateReceiver = newBroadcastReceiver(){
    @Override
    public void onReceive(Contextcontext,Intentintent){
      final Stringaction=intent.getAction();
      if(Stringaction.equal(Utils.ACTION_STOP_SCAN)){ ... }}};private IntentFilter makeGattUpdateIntentFilter(a) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Utils.ACTION_STOP_SCAN);
return intentFilter;
}

registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());

unregisterReceiver(mGattUpdateReceiver);Copy the code

That’s a lot simpler in a second

SharedPreferences

Using @SharedPref directly makes it easy to use the features of SharedPreferences. First, create a class to store the data you need to access:

@SharedPref(value=SharedPref.Scope.UNIQUE)
public interface MyPrefs {
    @DefaultBoolean(true)
    boolean isFirstIn(a);

    @DefaultString("")
    String ignoreVersion(a);

    @DefaultInt(0)
    int shockLevel(a);

}Copy the code

After the parentheses is the default value, then it is simple to use, first declared in the used class:

 @Pref
 MyPrefs_ myPrefs;

 boolean isFirstIn = myPrefs.isFirstIn().get();
 myPrefs.isFirstIn().put(false);Copy the code

SharedPref (value= sharedpref.scope.unique); SharedPref (value= sharedpref.scope.UNIQUE); SharedPref (value= sharedpref.scope. It took a long time to find out why.

@EBen

To use annotations in ordinary classes, simply add @eBean to the class name

@EBean
public class MyClass {
  @UiThread
  void updateUI(a) {}Copy the code

When used, declare:

@EActivity
public class MyActivity extends Activity {
  @Bean
  MyClass myClass;
}Copy the code

There are a few things to note: an @eBean annotated class can have only one constructor, and that constructor must have no arguments or only context arguments. Once declared in an activity or other component, you don’t need to go back to the new class, or you’ll get an error.

conclusion

Of course, there are many other annotations. If you want to know more about annotations, you can go to the link at the beginning of this article to check the official instructions for further understanding.