在上一篇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.对比
- 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(AdapterView<?> p, 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<String, Void, Bookmarks> {
- @Override
- protected Bookmarks doInBackground(String... params) {
- String searchString = params[0];
- String userId = params[1];
- RestTemplate client = new RestTemplate();
- HashMap<String, Object> args = new HashMap<String, Object>();
- args.put("search", searchString);
- args.put("userId", userId);
- HttpHeaders httpHeaders = new HttpHeaders();
- HttpEntity<Bookmarks> request = new HttpEntity<Bookmarks>(httpHeaders);
- ResponseEntity<Bookmarks> 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());
- }
- }
- @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());
- }
- }
需要注意的是androidannotation在编译时会生成一个子类,子类的名字是在原有的名字后面加"_",而且在AndroidManifest.xml中注册加"_"的类,例如:
- @Rest("http://www.bookmarks.com")
- public interface BookmarkClient {
- @Get("/bookmarks/{userId}?search={search}")
- Bookmarks getBookmarks(String search, String userId);
- }
<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
如有问题请留言,转载注明出处。
Android 最火快速开发框架androidannotation简介,布布扣,bubuko.com
Android 最火快速开发框架androidannotation简介
原文地址:http://blog.csdn.net/rain_butterfly/article/details/38733607