Android Tools-annotations

Source: Internet
Author: User

In the current use of Java, annotations has been widely used to improve the efficiency of development. In Android, the main thing is to help developers to deal with a number of front and back tasks, rest services, application classes, code snippets, etc., so that developers focus on what really matters.

(a) How to use Android annotations

Please refer to this article for the specific use method.


(ii) Examples of use

/** * Android Annotations Example * * @author Peter_wang * @create-time 2014-9-13 7:40:56 *///Set the activity layout file @eactivity (r.layout.activity_http_request) public class Httprequestactivity extends activity {//equals Findviewbyid, name default is ID name @    Viewbyid TextView Tv_response_main;    string resource file Information @StringRes (R.string.hello_world) string Mhelloworld;    Action @AfterViews void init () {Tv_response_main.settext (Mhelloworld) after execution of OnCreate;    }//Click event, Tv_response_main represents the clicked Trigger View object id @Click void tv_response_mainclicked () {gethttpdata (); }//Background execution thread @Background void Gethttpdata () {try {//Get HttpClient object HttpClient GetC            lient = new Defaulthttpclient ();             Get HttpGet object HttpGet request = new HttpGet ("http://192.168.140.1:8080/SimpleServer/servlet/PayServlet?id=1");            The client uses get method to carry out consult, obtains the server side response response HttpResponse response = Getclient.execute (request); Determine if the request was successful            if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get input stream INP                Utstreamreader 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 input stream instrem.close ();        } else {}} catch (Exception e) {e.printstacktrace ();    Update UI @UiThread void Sethttpdataui (String data) {Tv_response_main.settext (data) in//thread; }}


(iii) Working principle

Android annotations originates from Java annotations, and a simple understanding of Java annotations can be seen in this article.

There are three types of Java annotations: SourceCode, Class, Runtime.

Android Annotations is a sourcecode type that uses the Java Annotation processing Tool (APT) to compile the source file (*.java) by annotating the processor ( Annotationprocessor) interprets and processes the annotations in the source file, generates some new source files, and apt compiles the new generated source files until no new files are generated. The newly generated source files are in the Apt_generated folder.

Before compiling

Package com.some.company; @EActivitypublic class MyActivity extends Activity {  //...}

after compilation

Package Com.some.company;public Final class Myactivity_ extends MyActivity {  //...}
As a result : Activity generates new activity_ files, so the activity and application used in Androidmanifest.xml are underlined: xxactivity_, Xxapplication_.


(d) Advantages

(1) Simplify development and improve efficiency. Dependency Injection layout, views, resource, service and other common operations.

(2) Open. The generated source files are visible.

(3) does not affect performance. Because it is not runtime operation, the new source files are generated at compile time, which has no effect on the running speed of the program.


Android Tools-annotations

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.