Android tool-annotations
Currently, annotations are widely used in java to improve development efficiency. In android, it mainly helps developers process front and back-end tasks, rest services, application classes, code snippets, and so on, so that developers can focus on what really matters.
(1) how to use android annotations
For more information, see this document.
(2) Example
/*** Example of android annotations ** @ author peter_wang * @ create-time 7:40:56 * // set the layout file of the Activity @ EActivity (R. layout. activity_http_request) public class HttpRequestActivity extends Activity {// equivalent to findviewById. The default name is id name @ ViewById TextView TV _response_main; // string resource file information @ StringRes (R. string. hello_world) String mHelloWorld; // The action after oncreate is executed @ AfterViews void init () {TV _response_main. SetText (mHelloWorld);} // Click event. TV _response_main indicates the clicked View object id @ Click void TV _response_mainClicked () {getHttpData ();} // Background execution thread @ Background void getHttpData () {try {// get the HttpClient object HttpClient getClient = new DefaultHttpClient (); // get the HttpGet object HttpGet request = new HttpGet ("http: // 192.168.140.1: 8080/SimpleServer/servlet/PayServlet? Id = 1 "); // The client uses the GET method to obtain the server response HttpResponse response = getClient.exe cute (request); // determine whether the request is successful if (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// obtain the input stream InputStreamReader inStrem = new InputStreamReader (response. getEntity (). getContent (); BufferedReader br = new BufferedReader (inStrem); StringBuilder sb = new StringBuilder (); String readLine = null; while (readLine = Br. readLine ())! = Null) {sb. append (readLine);} setHttpDataUI (sb. toString (); // close the inStrem of the input stream. close () ;}else {}} catch (Exception e) {e. printStackTrace () ;}// update UI @ UiThread void setHttpDataUI (String data) {TV _response_main.setText (data);} in the thread );}}
(3) Working Principle
Android annotations originated from java annotations. You can read this article for a brief understanding of java annotations.
Java annotations contains three types: SourceCode, Class, and Runtime.
Android annotations are of the SourceCode type. Java Annotation Processing Tool (APT) is used to compile the source file (*. java) previously, through the annotation processor (AnnotationProcessor) to interpret and process the annotation in the source file, to generate some new source files, APT will also compile the new source files until no new files are generated. The newly generated source file is in the apt_generated folder.
Before Compilation
package com.some.company;@EActivitypublic class MyActivity extends Activity { // ...}
After compilation
package com.some.company;public final class MyActivity_ extends MyActivity { // ...}
The Activity generates a new Activity _ file. Therefore, the Activity and Application used in AndroidManifest. xml must be underlined: xxActivity _, xxApplication _.
(4) Advantages
(1) simplified development and improved efficiency. Dependencies are injected into layout, views, resources, services, and other common operations.
(2) open. The generated source file is visible.
(3) performance is not affected. Because it is not a runtime operation, new source files are generated during the compilation period, which does not affect the running speed.