android工具-annotations

来源:互联网 发布:怎么看淘宝收货时间 编辑:程序博客网 时间:2024/06/05 05:55

在当下的java的使用中,annotations已经被广泛运用,来提升开发效率。在android中,主要是帮助开发者处理一些前后台任务、rest 服务、应用类、代码片段等,让开发者专注于真正重要的东西。

(一)如何使用android annotations

具体使用方法请参看此文。


(二)使用范例

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * android annotations范例 
  3.  * 
  4.  * @author peter_wang 
  5.  * @create-time 2014-9-13 下午7:40:56 
  6.  */  
  7. // 设置Activity的layout布局文件  
  8. @EActivity(R.layout.activity_http_request)  
  9. public class HttpRequestActivity  
  10.     extends Activity {  
  11.     // 等同findviewById,名字默认是id名字  
  12.     @ViewById  
  13.     TextView tv_response_main;  
  14.     //string资源文件信息  
  15.     @StringRes(R.string.hello_world)  
  16.     String mHelloWorld;  
  17.   
  18.     // 执行完oncreate后的动作  
  19.     @AfterViews  
  20.     void init() {  
  21.         tv_response_main.setText(mHelloWorld);  
  22.     }  
  23.   
  24.     // 点击事件,tv_response_main代表点击的触发View对象id  
  25.     @Click  
  26.     void tv_response_mainClicked() {  
  27.         getHttpData();  
  28.     }  
  29.   
  30.     // 后台执行线程  
  31.     @Background  
  32.     void getHttpData() {  
  33.         try {  
  34.             // 得到HttpClient对象  
  35.             HttpClient getClient = new DefaultHttpClient();  
  36.             // 得到HttpGet对象  
  37.             HttpGet request = new HttpGet("http://192.168.140.1:8080/SimpleServer/servlet/PayServlet?id=1");  
  38.             // 客户端使用GET方式执行请教,获得服务器端的回应response  
  39.             HttpResponse response = getClient.execute(request);  
  40.             // 判断请求是否成功  
  41.             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  42.                 // 获得输入流  
  43.                 InputStreamReader inStrem = new InputStreamReader(response.getEntity().getContent());  
  44.                 BufferedReader br = new BufferedReader(inStrem);  
  45.                 StringBuilder sb = new StringBuilder();  
  46.                 String readLine = null;  
  47.                 while ((readLine = br.readLine()) != null) {  
  48.                     sb.append(readLine);  
  49.                 }  
  50.                 setHttpDataUI(sb.toString());  
  51.                 // 关闭输入流  
  52.                 inStrem.close();  
  53.             }  
  54.             else {  
  55.             }  
  56.         }  
  57.         catch (Exception e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61.   
  62.     // 线程中更新UI  
  63.     @UiThread  
  64.     void setHttpDataUI(String data) {  
  65.         tv_response_main.setText(data);  
  66.     }  
  67. }  


(三)工作原理

android annotations源于java annotations,简单了解java annotations可以看这篇文章。

java annotations中包含三种类型:SourceCode、Class、Runtime。

android annotations是SourceCode类型,利用Java Annotation Processing Tool (APT) 在编译源文件(*.java)之前,通过注解处理器(AnnotationProcessor)解释并处理源文件中的注解,生成 一些新的源文件,APT也会对新生成源文件进行编译,直到没有新的文件生成。新生成的源文件在apt_generated文件夹中。

编译前

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.some.company;  
  2. @EActivity  
  3. public class MyActivity extends Activity {  
  4.   // ...  
  5. }  

编译后

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.some.company;  
  2. public final class MyActivity_ extends MyActivity {  
  3.   // ...  
  4. }  
由此可知:Activity会生成新的Activity_文件,所以AndroidManifest.xml中用到的Activity和Application都要加下划线:xxActivity_,xxApplication_。


(四)优点

(1)简化开发,提高效率。依赖注入layout、views、resource、service等常用操作。

(2)开放。生成的源文件可见。

(3)不影响性能。因为不是runtime操作,都在编译期生成新的源文件,对程序运行速度没有影响。

0 0