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
- public class BookmarksToClipboardActivity extends Activity {
-
- BookmarkAdapter adapter;
-
- ListView bookmarkList;
-
- EditText search;
-
- BookmarkApplication application;
-
- Animation fadeIn;
-
- ClipboardManager clipboardManager;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN);
-
- setContentView(R.layout.bookmarks);
-
- bookmarkList = (ListView) findViewById(R.id.bookmarkList);
- search = (EditText) findViewById(R.id.search);
- application = (BookmarkApplication) getApplication();
- fadeIn = AnimationUtils.loadAnimation(this, anim.fade_in);
- clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
-
- View updateBookmarksButton1 = findViewById(R.id.updateBookmarksButton1);
- updateBookmarksButton1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- updateBookmarksClicked();
- }
- });
-
- View updateBookmarksButton2 = findViewById(R.id.updateBookmarksButton2);
- updateBookmarksButton2.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- updateBookmarksClicked();
- }
- });
-
- bookmarkList.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterViewp, View v, int pos, long id) {
- Bookmark selectedBookmark = (Bookmark) p.getAdapter().getItem(pos);
- bookmarkListItemClicked(selectedBookmark);
- }
- });
-
- initBookmarkList();
- }
-
- void initBookmarkList() {
- adapter = new BookmarkAdapter(this);
- bookmarkList.setAdapter(adapter);
- }
-
- void updateBookmarksClicked() {
- UpdateBookmarksTask task = new UpdateBookmarksTask();
-
- task.execute(search.getText().toString(), application.getUserId());
- }
-
- private static final String BOOKMARK_URL = //
- http://www.bookmarks.com/bookmarks/{userId}?search={search};
-
-
- class UpdateBookmarksTask extends AsyncTask {
-
- @Override
- protected Bookmarks doInBackground(String... params) {
- String searchString = params[0];
- String userId = params[1];
-
- RestTemplate client = new RestTemplate();
- HashMap args = new HashMap();
- args.put(search, searchString);
- args.put(userId, userId);
- HttpHeaders httpHeaders = new HttpHeaders();
- HttpEntity request = new HttpEntity(httpHeaders);
- ResponseEntity response = client.exchange( //
- BOOKMARK_URL, HttpMethod.GET, request, Bookmarks.class, args);
- Bookmarks bookmarks = response.getBody();
-
- return bookmarks;
- }
-
- @Override
- protected void onPostExecute(Bookmarks result) {
- adapter.updateBookmarks(result);
- bookmarkList.startAnimation(fadeIn);
- }
-
- }
-
- void bookmarkListItemClicked(Bookmark selectedBookmark) {
- clipboardManager.setText(selectedBookmark.getUrl());
- }
-
- }
After
- @NoTitle
- @Fullscreen
- @EActivity(R.layout.bookmarks)
- public class BookmarksToClipboardActivity extends Activity {
-
- BookmarkAdapter adapter;
-
- @ViewById
- ListView bookmarkList;
-
- @ViewById
- EditText search;
-
- @App
- BookmarkApplication application;
-
- @RestService
- BookmarkClient restClient;
-
- @AnimationRes
- Animation fadeIn;
-
- @SystemService
- ClipboardManager clipboardManager;
-
- @AfterViews
- void initBookmarkList() {
- adapter = new BookmarkAdapter(this);
- bookmarkList.setAdapter(adapter);
- }
-
- @Click({R.id.updateBookmarksButton1, R.id.updateBookmarksButton2})
- void updateBookmarksClicked() {
- searchAsync(search.getText().toString(), application.getUserId());
- }
-
- @Background
- void searchAsync(String searchString, String userId) {
- Bookmarks bookmarks = restClient.getBookmarks(searchString, userId);
- updateBookmarks(bookmarks);
- }
-
- @UiThread
- void updateBookmarks(Bookmarks bookmarks) {
- adapter.updateBookmarks(bookmarks);
- bookmarkList.startAnimation(fadeIn);
- }
-
- @ItemClick
- void bookmarkListItemClicked(Bookmark selectedBookmark) {
- clipboardManager.setText(selectedBookmark.getUrl());
- }
-
- }
- @Rest(http://www.bookmarks.com)
- public interface BookmarkClient {
-
- @Get(/bookmarks/{userId}?search={search})
- Bookmarks getBookmarks(String search, String userId);
-
- }
需要注意的是androidannotation在編譯時會生成一個子類,子類的名字是在原有的名字後面加_,而且在AndroidManifest.xml中注冊加_的類,例如:
跳轉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
如有問題請留言,轉載注明出處。