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
- 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.exe cute (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 );
-
- }
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.