AndroidAnnotations open-source framework

Source: Internet
Author: User

AndroidAnnotations open-source framework

To learn other people's source code during this period, we need the AndroidAnnotations open-source framework. His location is: Fast Android Development, as said on his homepage on github. easy maintenance. it has the advantages of fast Android development and easy maintenance.

Features:

Features
  • Dependency injection: inject views, extras, system services, resources,... (DI: Dependency injection)
  • Simplified threading model: annotate your methods so that they execute on the UI thread or on a background thread. (Simplified thread model)
  • Event binding: annotate methods to handle events on views, no more uugly anonymous listener classes! (Event binding)
  • REST client: create a client interface, AndroidAnnotations generates the implementation.
  • No magic: As AndroidAnnotations generate subclasses at compile time, you can check the code to see how it works.
  • AndroidAnnotations provide those good things and even more for less than 50kb, without any runtime perf impact! Let's take a look at the following code:
    @EActivity(R.layout.translate) // Sets content view to R.layout.translatepublic class TranslateActivity extends Activity {    @ViewById // Injects R.id.textInput    EditText textInput;    @ViewById(R.id.myTextView) // Injects R.id.myTextView    TextView result;    @AnimationRes // Injects android.R.anim.fade_in    Animation fadeIn;    @Click // When R.id.doTranslate button is clicked     void doTranslate() {         translateInBackground(textInput.getText().toString());    }    @Background // Executed in a background thread    void translateInBackground(String textToTranslate) {         String translatedText = callGoogleTranslate(textToTranslate);         showResult(translatedText);    }    @UiThread // Executed in the ui thread    void showResult(String translatedText) {         result.setText(translatedText);         result.startAnimation(fadeIn);    }    // [...]}
    // ================================================ ==================================

    AndroidAnnotations works in a very simple way. It automatically adds an extra compilation step that generates source code, using the standard Java Annotation Processing Tool.

    What source code? For each enhanced class, for example each@EActivityAnnotated activity, a subclass of this activity is generated, with the same name plus an underscore appended at the end. (a subclass is generated)

    For instance, the following class:

    package com.some.company;@EActivitypublic class MyActivity extends Activity {  // ...}

    Will generate the following subclass, in the same package but in another source folder :( generate a subclass of MyActivity)

    package com.some.company;public final class MyActivity_ extends MyActivity {  // ...}

    This subclass adds behavior to your activity by overriding some methods (for instanceonCreate()), Yet delegating the cballs to super.

    That is the reason why you must add_To your activity names inAndroidManifest.xml(The subclass generated in the configuration file)

    Starting an annotated activity

    In Android, you usually start an activity this way:

    startActivity(this, MyListActivity.class);

    However, with AndroidAnnotations, the real activity that must be started isMyListActivity_:

    startActivity(this, MyListActivity_.class);
    Intent Builder

    Since AndroidAnnotations 2.4

    We provide a static helper to let you start the generated activity:

    // Starting the activityMyListActivity_.intent(context).start();// Building an intent from the activityIntent intent = MyListActivity_.intent(context).get();// You can provide flagsMyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();// You can even provide extras defined with @Extra in the activityMyListActivity_.intent(context).myDateExtra(someDate).start();

    Since AndroidAnnotations 2.7

    You can also usestartActivityForResult()Equivalent:

    MyListActivity_.intent(context).startForResult();
    Starting an annotated Service

    In Android, you usually start a service this way:

    startService(this, MyService.class);

    However, with AndroidAnnotations, the real Service that must be started isMyService_:

    startService(this, MyService_.class);
    Intent Builder

    Since AndroidAnnotations 2.7

    We provide a static helper to let you start the generated service:

    // Starting the serviceMyService_.intent(context).start();// Building an intent from the activityIntent intent = MyService_.intent(context).build();// You can provide flagsMyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start(
    AvailableAnnotationsEnhanced components
    • @ EActivity
    • @ EApplication
    • @ EBean
    • @ EFragment
    • @ EProvider
    • @ EReceiver
    • @ EIntentService
    • @ EService
    • @ EView
    • @ EViewGroupInjection
      • @ AfterExtras
      • @ AfterInject
      • @ AfterViews
      • @ App
      • @ Bean
      • @ Extra
      • @ FragmentArg
      • @ FragmentById
      • @ FragmentByTag
      • @ FromHtml
      • @ HttpsClient
      • @ NonConfigurationInstance
      • @ RootContext
      • @ SystemService
      • @ ViewById
      • @ ViewsByIdEvent binding
        • @ TextChange
        • @ AfterTextChange
        • @ BeforeTextChange
        • @ Editexception
        • @ FocusChange
        • @ CheckedChange
        • @ Touch
        • @ Click
        • @ LongClick
        • @ ItemClick
        • @ ItemLongClick
        • @ ItemSelect
        • @ OptionsItem
        • @ SeekBarProgressChange
        • @ SeekBarTouchStart
        • @ SeekBarTouchStopThreading
          • @ Background
          • @ UiThread
          • @ SupposeBackground
          • @ SupposeUiThreadMisc
            • @ InstanceState
            • @ WindowFeature
            • @ Fullscreen
            • @ NoTitle
            • @ CustomTitle
            • @ OptionsMenu
            • @ OptionsMenuItem
            • @ OrmLiteDao
            • @ RoboGuice
            • @ Trace
            • @ Transactional
            • @ OnActivityResult
            • @ OnActivityResult. Extra
            • @ HierarchyViewerSupport
            • @ ServiceAction
            • @ Receiver
            • @ ReceiverAction
            • @ ReceiverAction. Extra
            • @ IgnoredWhenDetached
            • @ WakeLockResource injection
              • @ StringRes
              • @ AnimationRes
              • @ ColorRes
              • @ DimensionPixelOffsetRes
              • @ DimensionPixelSizeRes
              • @ DimensionRes
              • @ BooleanRes
              • @ ColorStateListRes
              • @ DrawableRes
              • @ IntArrayRes
              • @ IntegerRes
              • @ LayoutRes
              • @ MovieRes
              • @ StringArrayRes
              • @ TextArrayRes
              • @ TextRes
              • @ HtmlResRest API
                • @ Rest
                • @ RestService
                • @ Get
                • @ Post
                • @ Put
                • @ Delete
                • @ Options
                • @ Head
                • @ Accept
                • @ RequiresHeader
                • @ RequiresCookie
                • @ RequiresCookieInUrl
                • @ RequiresAuthentication
                • @ SetsCookie
                • @ RequiresCookieInUrlTypesafe SharedPreferences
                  • @ Defaboolean Boolean
                  • @ Defaflofloat
                  • @ DefaultInt
                  • @ DefaultLong
                  • @ DefaultString
                  • @ Defastrstringset
                  • @ DefaultRes
                  • @ Pref
                  • @ SharedPref: This is where you can use it to view its documents in detail.








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.