Android 最火高速开发框架AndroidAnnotations简单介绍
在上一篇Android 最火的高速开发框架androidannotations配置具体解释中介绍了在eclipse中配置androidannotation的步骤,如需配置请參考。
1.目标
androidannotation框架要促进Android应用程序的编写和维护。相信简单的代码有明白的意图是实现这些目标的最佳途径。
当我们都沉浸在开发Android应用程序时,我们经常会想:为什么我们总是须要重复写同样的代码吗?为什么我们的应用越来越难维护呢?
context和activity就像是神一样的东西,我们被复杂的线程玩耍,难以參考的API,载入非常多的无名监听类,写大量不须要的异常抛出..我们不能改善它们吗?
2.怎么改善
利用Java 注解机制,开发人员能展示他们的意图,还能让androidannotation框架在编译时继承管道代码。
3.特色
*依赖注入:注入view、extras数据、service、resource..
*简单的线程模型:在方法名上凝视是在UI线程执行还是后台线程执行
*事件绑定:凝视你的方法处理view的事件,不再有难看的匿名监听类。
*RESTclient:创建了一个client接口,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(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());
- }
- }
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());
- }
- }
须要注意的是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
如有问题请留言,转载注明出处。