About androidannotation, the most popular and fast development framework for Android

Source: Internet
Author: User

About androidannotation, the most popular and fast development framework for Android

 

 

1. Objectives

 

The androidannotation framework should facilitate the compilation and maintenance of Android applications. I believe that simple code with clear intentions is the best way to achieve these goals.

When we are all immersed in developing Android applications, we often think: Why do we always need to write the same code repeatedly? Why is it increasingly difficult to maintain our applications?

Context and activity are like things of God. We are played by complicated threads, difficult to reference APIs, loading a lot of unknown Listening Classes, and writing a lot of unnecessary exceptions .. can't we improve them?

 

2. How to improve

 

Using the Java annotation mechanism, developers can demonstrate their intentions and allow the androidannotation framework to inherit Pipeline Code during compilation.

 

3. Features

 

* Dependency injection: injection of view, extras data, service, resource ..

* Simple thread model: Specifies whether to run in the UI thread or in the background thread on the method name.

* Event binding: annotate your method to process view events. There are no ugly anonymous Listening Classes.

* REST client: Creates a client interface. androidannotation inherits the springAndroid interface.

* Not unfathomable: When the AndroidAnnotations inheritance subclass is compiled, you can check the code to see how it works.

* AndroidAnnotation provides so many good things, even smaller than 50kb, without any performance impact during runtime.

 

4. Some applications using androidannotation

 

 

5. Comparison

 

 

Before
 
  1. Public class BookmarksToClipboardActivity extends Activity {
  2.  
  3. BookmarkAdapter adapter;
  4.  
  5. ListView bookmarkList;
  6.  
  7. EditText search;
  8.  
  9. BookmarkApplication application;
  10.  
  11. Animation fadeIn;
  12.  
  13. ClipboardManager clipboardManager;
  14.  
  15. @ Override
  16. Protected void onCreate (Bundle savedInstanceState ){
  17. Super. onCreate (savedInstanceState );
  18.  
  19. RequestWindowFeature (Window. FEATURE_NO_TITLE );
  20. GetWindow (). setFlags (FLAG_FULLSCREEN, FLAG_FULLSCREEN );
  21.  
  22. SetContentView (R. layout. bookmarks );
  23.  
  24. BookmarkList = (ListView) findViewById (R. id. bookmarkList );
  25. Search = (EditText) findViewById (R. id. search );
  26. Application = (BookmarkApplication) getApplication ();
  27. FadeIn = AnimationUtils. loadAnimation (this, anim. fade_in );
  28. ClipboardManager = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE );
  29.  
  30. View updateBookmarksButton1 = findViewById (R. id. updateBookmarksButton1 );
  31. UpdateBookmarksButton1.setOnClickListener (new OnClickListener (){
  32.  
  33. @ Override
  34. Public void onClick (View v ){
  35. UpdateBookmarksClicked ();
  36. }
  37. });
  38.  
  39. View updateBookmarksButton2 = findViewById (R. id. updateBookmarksButton2 );
  40. UpdateBookmarksButton2.setOnClickListener (new OnClickListener (){
  41.  
  42. @ Override
  43. Public void onClick (View v ){
  44. UpdateBookmarksClicked ();
  45. }
  46. });
  47.  
  48. BookmarkList. setOnItemClickListener (new OnItemClickListener (){
  49.  
  50. @ Override
  51. Public void onItemClick (AdapterView P, View v, int pos, long id ){
  52. Bookmark selectedBookmark = (Bookmark) p. getAdapter (). getItem (pos );
  53. BookmarkListItemClicked (selectedBookmark );
  54. }
  55. });
  56.  
  57. InitBookmarkList ();
  58. }
  59.  
  60. Void initBookmarkList (){
  61. Adapter = new BookmarkAdapter (this );
  62. BookmarkList. setAdapter (adapter );
  63. }
  64.  
  65. Void updateBookmarksClicked (){
  66. UpdateBookmarksTask task = new UpdateBookmarksTask ();
  67.  
  68. Task.exe cute (search. getText (). toString (), application. getUserId ());
  69. }
  70.  
  71. Private static final String BOOKMARK_URL = //
  72. Http://www.bookmarks.com/bookmarks/?userid }? Search = {search };
  73.  
  74.  
  75. Class UpdateBookmarksTask extends AsyncTask {
  76.  
  77. @ Override
  78. Protected Bookmarks doInBackground (String... params ){
  79. String searchString = params [0];
  80. String userId = params [1];
  81.  
  82. RestTemplate client = new RestTemplate ();
  83. HashMap Args = new HashMap ();
  84. Args. put (search, searchString );
  85. Args. put (userId, userId );
  86. HttpHeaders httpHeaders = new HttpHeaders ();
  87. HttpEntity Request = new HttpEntity (HttpHeaders );
  88. ResponseEntity Response = client. exchange (//
  89. BOOKMARK_URL, HttpMethod. GET, request, Bookmarks. class, args );
  90. Bookmarks bookmarks = response. getBody ();
  91.  
  92. Return bookmarks;
  93. }
  94.  
  95. @ Override
  96. Protected void onPostExecute (Bookmarks result ){
  97. Adapter. updateBookmarks (result );
  98. BookmarkList. startAnimation (fadeIn );
  99. }
  100.  
  101. }
  102.  
  103. Void bookmarkListItemClicked (Bookmark selectedBookmark ){
  104. ClipboardManager. setText (selectedBookmark. getUrl ());
  105. }
  106.  
  107. }
After
 
  1. @ NoTitle
  2. @ Fullscreen
  3. @ EActivity (R. layout. bookmarks)
  4. Public class BookmarksToClipboardActivity extends Activity {
  5.  
  6. BookmarkAdapter adapter;
  7.  
  8. @ ViewById
  9. ListView bookmarkList;
  10.  
  11. @ ViewById
  12. EditText search;
  13.  
  14. @ App
  15. BookmarkApplication application;
  16.  
  17. @ RestService
  18. BookmarkClient restClient;
  19.  
  20. @ AnimationRes
  21. Animation fadeIn;
  22.  
  23. @ SystemService
  24. ClipboardManager clipboardManager;
  25.  
  26. @ AfterViews
  27. Void initBookmarkList (){
  28. Adapter = new BookmarkAdapter (this );
  29. BookmarkList. setAdapter (adapter );
  30. }
  31.  
  32. @ Click ({R. id. updateBookmarksButton1, R. id. updateBookmarksButton2 })
  33. Void updateBookmarksClicked (){
  34. SearchAsync (search. getText (). toString (), application. getUserId ());
  35. }
  36.  
  37. @ Background
  38. Void searchAsync (String searchString, String userId ){
  39. Bookmarks bookmarks = restClient. getBookmarks (searchString, userId );
  40. UpdateBookmarks (bookmarks );
  41. }
  42.  
  43. @ UiThread
  44. Void updateBookmarks (Bookmarks bookmarks ){
  45. Adapter. updateBookmarks (bookmarks );
  46. BookmarkList. startAnimation (fadeIn );
  47. }
  48.  
  49. @ ItemClick
  50. Void bookmarkListItemClicked (Bookmark selectedBookmark ){
  51. ClipboardManager. setText (selectedBookmark. getUrl ());
  52. }
  53.  
  54. }
 
  1. @ Rest (http://www.bookmarks.com)
  2. Public interface BookmarkClient {
  3.  
  4. @ Get (/bookmarks/{userId }? Search = {search })
  5. Bookmarks getBookmarks (String search, String userId );
  6.  
  7. }
It should be noted that androidannotation will generate a subclass during compilation. The subclass name is _ added after the original name and _ is registered in AndroidManifest. xml. For example:
 

 

The jump to activity is also different:

 

startActivity(this, MyListActivity_.class);
A simple method is provided after version 2.4:

 

 

MyListActivity_.intent(context).start();
Versions later than 2.7 can be used:

 

 

MyListActivity_.intent(context).startForResult();
Enabling service is similar:

 

 

MyService_.intent(context).start();

For more instructions, see the official documentation.

Https://github.com/excilys/androidannotations/wiki/Cookbook

 

If you have any questions, please leave a message and repeat the source.

 

 

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.