Android 最火快速开发框架androidannotation简介

来源:互联网 发布:mac imovie不见了 编辑:程序博客网 时间:2024/05/01 04:47

在上一篇 Android 最火的快速开发框架androidannotations配置详解 中介绍了在eclipse中配置androidannotation的步骤,如需配置请参考。

1.目标

androidannotation框架 要促进Android应用程序的编写和维护。 相信简单的代码有明确的意图是实现这些目标的最佳途径。

    当 我们都沉浸在开发Android应用程序时,我们常常会想:为什么我们总是需要反复写相同的代码吗? 为什么我们的应用越来越难维护呢?

   context和activity就像是神一样的东西,我们被复杂的线程玩耍,难以参考的API,加载很多的无名监听类,写大量不需要的异常抛出..我们不能改善它们吗?

2.怎么改善

    利用Java 注解机制,开发者能展示他们的意图,还能让androidannotation框架在编译时继承管道代码。

3.特色

*依赖注入:注入view、extras数据、service、resource..

*简单的线程模型:在方法名上注释是在UI线程运行还是后台线程运行

*事件绑定:注释你的方法处理view的事件,不再有难看的匿名监听类。

*REST客户端:创建了一个客户端接口, androidannotation继承了springAndroid的接口。

*不是深不可测:因为AndroidAnnotations继承子类在编译时,你可以检查代码看看它是怎么工作的。

*AndroidAnnotation提供了这么多的好东西甚至它是小于50kb,在运行时没有任何性能影响。

4.使用androidannotation的一些应用

5.对比

Before

  1. public class BookmarksToClipboardActivity extends Activity {
  2. BookmarkAdapter adapter ;
  3.  
  4. ListView bookmarkList ;
  5.  
  6. EditText search ;
  7.  
  8. BookmarkApplication application ;
  9.  
  10. Animation fadeIn ;
  11.  
  12. ClipboardManager clipboardManager ;
  13.  
  14. @Override
  15. protected void onCreate ( Bundle savedInstanceState ) {
  16. super . onCreate ( savedInstanceState );
  17.  
  18. requestWindowFeature ( Window . FEATURE_NO_TITLE );
  19. getWindow (). setFlags ( FLAG_FULLSCREEN , FLAG_FULLSCREEN );
  20.  
  21. setContentView ( R . layout . bookmarks );
  22.  
  23. bookmarkList = ( ListView ) findViewById ( R . id . bookmarkList );
  24. search = ( EditText ) findViewById ( R . id . search );
  25. application = ( BookmarkApplication ) getApplication ();
  26. fadeIn = AnimationUtils . loadAnimation ( this , anim . fade_in );
  27. clipboardManager = ( ClipboardManager ) getSystemService (CLIPBOARD_SERVICE );
  28.  
  29. View updateBookmarksButton1 = findViewById ( R . id .updateBookmarksButton1 );
  30. updateBookmarksButton1 . setOnClickListener ( new OnClickListener () {
  31.  
  32. @Override
  33. public void onClick ( View v ) {
  34. updateBookmarksClicked ();
  35. }
  36. });
  37.  
  38. View updateBookmarksButton2 = findViewById ( R . id .updateBookmarksButton2 );
  39. updateBookmarksButton2 . setOnClickListener ( new OnClickListener () {
  40.  
  41. @Override
  42. public void onClick ( View v ) {
  43. updateBookmarksClicked ();
  44. }
  45. });
  46.  
  47. bookmarkList . setOnItemClickListener ( new OnItemClickListener () {
  48.  
  49. @Override
  50. public void onItemClick ( AdapterView <?> p , View v , int pos , long id ) {
  51. Bookmark selectedBookmark = ( Bookmark ) p . getAdapter (). getItem ( pos );
  52. bookmarkListItemClicked ( selectedBookmark );
  53. }
  54. });
  55.  
  56. initBookmarkList ();
  57. }
  58.  
  59. void initBookmarkList () {
  60. adapter = new BookmarkAdapter ( this );
  61. bookmarkList . setAdapter ( adapter );
  62. }
  63.  
  64. void updateBookmarksClicked () {
  65. UpdateBookmarksTask task = new UpdateBookmarksTask ();
  66.  
  67. task . execute ( search . getText (). toString (), application . getUserId ());
  68. }
  69. private static final String BOOKMARK_URL = //
  70. "http://www.bookmarks.com/bookmarks/{userId}?search={search}" ;
  71. class UpdateBookmarksTask extends AsyncTask < String , Void , Bookmarks > {
  72.  
  73. @Override
  74. protected Bookmarks doInBackground ( String ... params ) {
  75. String searchString = params [ 0 ];
  76. String userId = params [ 1 ];
  77.  
  78. RestTemplate client = new RestTemplate ();
  79. HashMap < String , Object > args = new HashMap < String , Object >();
  80. args . put ( "search" , searchString );
  81. args . put ( "userId" , userId );
  82. HttpHeaders httpHeaders = new HttpHeaders ();
  83. HttpEntity < Bookmarks > request = new HttpEntity < Bookmarks >( httpHeaders);
  84. ResponseEntity < Bookmarks > response = client . exchange ( //
  85. BOOKMARK_URL , HttpMethod . GET , request , Bookmarks . class , args );
  86. Bookmarks bookmarks = response . getBody ();
  87.  
  88. return bookmarks ;
  89. }
  90.  
  91. @Override
  92. protected void onPostExecute ( Bookmarks result ) {
  93. adapter . updateBookmarks ( result );
  94. bookmarkList . startAnimation ( fadeIn );
  95. }
  96. }
  97.  
  98. void bookmarkListItemClicked ( Bookmark selectedBookmark ) {
  99. clipboardManager . setText ( selectedBookmark . getUrl ());
  100. }
  101.  
  102. }

After

  1. @NoTitle
  2. @Fullscreen
  3. @EActivity ( R . layout . bookmarks )
  4. public class BookmarksToClipboardActivity extends Activity {
  5. BookmarkAdapter adapter ;
  6. @ViewById
  7. ListView bookmarkList ;
  8.  
  9. @ViewById
  10. EditText search ;
  11. @App
  12. BookmarkApplication application ;
  13. @RestService
  14. BookmarkClient restClient ;
  15.  
  16. @AnimationRes
  17. Animation fadeIn ;
  18. @SystemService
  19. ClipboardManager clipboardManager ;
  20.  
  21. @AfterViews
  22. void initBookmarkList () {
  23. adapter = new BookmarkAdapter ( this );
  24. bookmarkList . setAdapter ( adapter );
  25. }
  26. @Click ({ R . id . updateBookmarksButton1 , R . id . updateBookmarksButton2 })
  27. void updateBookmarksClicked () {
  28. searchAsync ( search . getText (). toString (), application . getUserId ());
  29. }
  30. @Background
  31. void searchAsync ( String searchString , String userId ) {
  32. Bookmarks bookmarks = restClient . getBookmarks ( searchString , userId );
  33. updateBookmarks ( bookmarks );
  34. }
  35.  
  36. @UiThread
  37. void updateBookmarks ( Bookmarks bookmarks ) {
  38. adapter . updateBookmarks ( bookmarks );
  39. bookmarkList . startAnimation ( fadeIn );
  40. }
  41. @ItemClick
  42. void bookmarkListItemClicked ( Bookmark selectedBookmark ) {
  43. clipboardManager . setText ( selectedBookmark . getUrl ());
  44. }
  45.  
  46. }
  1. @Rest ( "http://www.bookmarks.com" )
  2. public interface BookmarkClient {
  3. @Get ( "/bookmarks/{userId}?search={search}" )
  4. Bookmarks getBookmarks ( String search , String userId );
  5.  
  6. }
需要注意的是androidannotation在编译时会生成一个子类,子类的名字是在原有的名字后面加"_",而且在AndroidManifest.xml中注册加"_"的类,例如:
<activity android:name=".MyListActivity_" />

跳转activity时也有所不同:

startActivity(this, MyListActivity_.class);
从2.4版本后提供了一个简便方法:
MyListActivity_.intent(context).start();
2.7版本之后可以使用:
MyListActivity_.intent(context).startForResult();
开启service类似:
MyService_.intent(context).start();

更多的注解使用方法请参考官方文档。

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

如有问题请留言,转载注明出处。

0 0
原创粉丝点击