Android tool-annotations

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.