android工具-annotations

來源:互聯網
上載者:User

android工具-annotations

在當下的java的使用中,annotations已經被廣泛運用,來提升開發效率。在android中,主要是協助開發人員處理一些前背景工作、rest 服務、應用類、程式碼片段等,讓開發人員專註於真正重要的東西。

(一)如何使用android annotations

具體使用方法請參看此文。


(二)使用範例

/** * android annotations範例 * * @author peter_wang * @create-time 2014-9-13 下午7:40:56 */// 設定Activity的layout布局檔案@EActivity(R.layout.activity_http_request)public class HttpRequestActivity    extends Activity {    // 等同findviewById,名字預設是id名字    @ViewById    TextView tv_response_main;    //string資源檔資訊    @StringRes(R.string.hello_world)    String mHelloWorld;    // 執行完oncreate後的動作    @AfterViews    void init() {        tv_response_main.setText(mHelloWorld);    }    // 點擊事件,tv_response_main代表點擊的觸發View對象id    @Click    void tv_response_mainClicked() {        getHttpData();    }    // 後台執行線程    @Background    void getHttpData() {        try {            // 得到HttpClient對象            HttpClient getClient = new DefaultHttpClient();            // 得到HttpGet對象            HttpGet request = new HttpGet("http://192.168.140.1:8080/SimpleServer/servlet/PayServlet?id=1");            // 用戶端使用GET方式執行請教,獲得伺服器端的回應response            HttpResponse response = getClient.execute(request);            // 判斷請求是否成功            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                // 獲得輸入資料流                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());                // 關閉輸入資料流                inStrem.close();            }            else {            }        }        catch (Exception e) {            e.printStackTrace();        }    }    // 線程中更新UI    @UiThread    void setHttpDataUI(String data) {        tv_response_main.setText(data);    }}


(三)工作原理

android annotations源於java annotations,簡單瞭解java annotations可以看這篇文章。

java annotations中包含三種類型:SourceCode、Class、Runtime。

android annotations是SourceCode類型,利用Java Annotation Processing Tool (APT) 在編譯源檔案(*.java)之前,通過註解處理器(AnnotationProcessor)解釋並處理源檔案中的註解,產生 一些新的源檔案,APT也會對新產生源檔案進行編譯,直到沒有新的檔案產生。新產生的源檔案在apt_generated檔案夾中。

編譯前

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

編譯後

package com.some.company;public final class MyActivity_ extends MyActivity {  // ...}
由此可知:Activity會產生新的Activity_檔案,所以AndroidManifest.xml中用到的Activity和Application都要加底線:xxActivity_,xxApplication_。


(四)優點

(1)簡化開發,提高效率。依賴注入layout、views、resource、service等常用操作。

(2)開放。產生的源檔案可見。

(3)不影響效能。因為不是runtime操作,都在編譯期產生新的源檔案,對程式運行速度沒有影響。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.