首页 > 代码库 > AndroidAnnotations开源框架
AndroidAnnotations开源框架
这段时间学习别人的源码,需要AndroidAnnotations这个开源的框架,学习下,他的地在:https://github.com/excilys/androidannotations文档的地址在:https://github.com/excilys/androidannotations/wiki 正如在github上他的主页上所说:Fast Android Development. Easy maintenance.它具有快速Android开发,易于维护的优点。
特点:
Features
- Dependency injection: inject views, extras, system services, resources, ...(DI:依赖注入)
- Simplified threading model: annotate your methods so that they execute on the UI thread or on a background thread.(简化的线程模型)
- Event binding: annotate methods to handle events on views, no more ugly anonymous listener classes!(事件绑定)
- 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!
@EActivity(R.layout.translate) // Sets content view to R.layout.translate public 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 @EActivity
annotated activity, a subclass of this activity is generated, with the same name plus an underscore appended at the end.(会生成一个子类)
For instance, the following class:
package com.some.company; @EActivity public class MyActivity extends Activity { // ... }
Will generate the following subclass, in the same package but in another source folder:(生成一个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 calls to super.
That is the reason why you must add _
to your activity names in AndroidManifest.xml
:(清单文件中这样配置生成的子类)
<activity android:name=".MyListActivity_" />
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 activity MyListActivity_.intent(context).start(); // Building an intent from the activity Intent intent = MyListActivity_.intent(context).get(); // You can provide flags MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start(); // You can even provide extras defined with @Extra in the activity MyListActivity_.intent(context).myDateExtra(someDate).start();
Since AndroidAnnotations 2.7
You can also use the startActivityForResult()
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 is MyService_
:
startService(this, MyService_.class);
Intent Builder
Since AndroidAnnotations 2.7
We provide a static helper to let you start the generated service:
// Starting the service MyService_.intent(context).start(); // Building an intent from the activity Intent intent = MyService_.intent(context).build(); // You can provide flags MyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start(
AvailableAnnotations
Enhanced components
- @EActivity
- @EApplication
- @EBean
- @EFragment
- @EProvider
- @EReceiver
- @EIntentService
- @EService
- @EView
- @EViewGroup
Injection
- @AfterExtras
- @AfterInject
- @AfterViews
- @App
- @Bean
- @Extra
- @FragmentArg
- @FragmentById
- @FragmentByTag
- @FromHtml
- @HttpsClient
- @NonConfigurationInstance
- @RootContext
- @SystemService
- @ViewById
- @ViewsById
Event binding
- @TextChange
- @AfterTextChange
- @BeforeTextChange
- @EditorAction
- @FocusChange
- @CheckedChange
- @Touch
- @Click
- @LongClick
- @ItemClick
- @ItemLongClick
- @ItemSelect
- @OptionsItem
- @SeekBarProgressChange
- @SeekBarTouchStart
- @SeekBarTouchStop
Threading
- @Background
- @UiThread
- @SupposeBackground
- @SupposeUiThread
Misc
- @InstanceState
- @WindowFeature
- @Fullscreen
- @NoTitle
- @CustomTitle
- @OptionsMenu
- @OptionsMenuItem
- @OrmLiteDao
- @RoboGuice
- @Trace
- @Transactional
- @OnActivityResult
- @OnActivityResult.Extra
- @HierarchyViewerSupport
- @ServiceAction
- @Receiver
- @ReceiverAction
- @ReceiverAction.Extra
- @IgnoredWhenDetached
- @WakeLock
Resource injection
- @StringRes
- @AnimationRes
- @ColorRes
- @DimensionPixelOffsetRes
- @DimensionPixelSizeRes
- @DimensionRes
- @BooleanRes
- @ColorStateListRes
- @DrawableRes
- @IntArrayRes
- @IntegerRes
- @LayoutRes
- @MovieRes
- @StringArrayRes
- @TextArrayRes
- @TextRes
- @HtmlRes
Rest API
- @Rest
- @RestService
- @Get
- @Post
- @Put
- @Delete
- @Options
- @Head
- @Accept
- @RequiresHeader
- @RequiresCookie
- @RequiresCookieInUrl
- @RequiresAuthentication
- @SetsCookie
- @RequiresCookieInUrl
Typesafe SharedPreferences
- @DefaultBoolean
- @DefaultFloat
- @DefaultInt
- @DefaultLong
- @DefaultString
- @DefaultStringSet
- @DefaultRes
- @Pref
- @SharedPref
AndroidAnnotations开源框架