<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在開發Android移動客戶端的時候往往要使用多線程來進行操作,我們通常會將耗時的操作放在單獨的線程執行,避免其占用主線程而給用戶帶來不好的用戶體驗。但是在子線程中無法去操作主線程(UI 線程),在子線程中操作UI線程會出現錯誤。因此android提供了一個類Handler來在子線程中來更新UI線程,用發消息的機制更新UI界面,呈現給用戶。這樣就解決了子線程更新UI的問題。但是費時的任務操作總會啟動一些匿名的子線程,太多的子線程給系統帶來巨大的負擔,隨之帶來一些性能問題。因此android提供了一個工具類AsyncTask,顧名思義異步執行任務。這個AsyncTask生來就是處理一些后臺的比較耗時的任務,給用戶帶來良好用戶體驗的,從編程的語法上顯得優雅了許多,不再需要子線程和Handler就可以完成異步操作并且刷新用戶界面。 先大概認識下Android.os.AsyncTask類: android的類AsyncTask對線程間通訊進行了包裝,提供了簡易的編程方式來使后臺線程和UI線程進行通訊:后臺線程執行異步任務,并把操作結果通知UI線程。 AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。 Params 啟動任務執行的輸入參數,比如HTTP請求的URL。 Progress 后臺任務執行的百分比。 Result 后臺執行任務最終返回的結果,比如String,Integer等。 AsyncTask的執行分為四個步驟,每一步都對應一個回調方法,開發者需要實現這些方法。 1) 繼承AsyncTask 2) 實現AsyncTask中定義的下面一個或幾個方法 onPreExecute(), 該方法將在執行實際的后臺操作前被UI 線程調用。可以在該方法中做一些準備工作,如在界面上顯示一個進度條,或者一些控件的實例化,這個方法可以不用實現。 doInBackground(Params...), 將在onPreExecute 方法執行后馬上執行,該方法運行在后臺線程中。這里將主要負責執行那些很耗時的后臺處理工作。可以調用 publishProgress方法來更新實時的任務進度。該方法是抽象方法,子類必須實現。 onProgressUpdate(Progress...),在publishProgress方法被調用后,UI 線程將調用這個方法從而在界面上展示任務的進展情況,例如通過一個進度條進行展示。 onPostExecute(Result), 在doInBackground 執行完成后,onPostExecute 方法將被UI 線程調用,后臺的計算結果將通過該方法傳遞到UI 線程,并且在界面上展示給用戶. onCancelled(),在用戶取消線程操作的時候調用。在主線程中調用onCancelled()的時候調用。 為了正確的使用AsyncTask類,以下是幾條必須遵守的準則: ??   1) Task的實例必須在UI 線程中創建 ??   2) execute方法必須在UI 線程中調用 ??   3) 不要手動的調用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個方法,需要在UI線程中實例化這個task來調用。 ??   4) 該task只能被執行一次,否則多次調用時將會出現異常 doInBackground方法和onPostExecute的參數必須對應,這兩個參數在AsyncTask聲明的泛型參數列表中指定,第一個為doInBackground接受的參數,第二個為顯示進度的參數,第第三個為doInBackground返回和onPostExecute傳入的參數。 下面通過一個Demo來說明如何使用Android.os.AsyncTask類,通過進度條來顯示進行的進度,然后用TextView來顯示進度值。程序結構圖如下: ![](https://box.kancloud.cn/2016-05-16_57397390e94bb.gif) [1] \layout\main.xml 布局文件源碼如下: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello , Welcome to Andy's Blog!"/> <Button android:id="@+id/download" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Download"/> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="當前進度顯示"/> <ProgressBar android:id="@+id/pb" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"/> </LinearLayout> ~~~ [2] /src中的MainActivity.java源碼如下: ~~~ package com.andyidea.demo; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { Button download; ProgressBar pb; TextView tv; /**Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pb=(ProgressBar)findViewById(R.id.pb); tv=(TextView)findViewById(R.id.tv); download = (Button)findViewById(R.id.download); download.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DownloadTask dTask = new DownloadTask(); dTask.execute(100); } }); } class DownloadTask extends AsyncTask<Integer, Integer, String>{ //后面尖括號內分別是參數(例子里是線程休息時間),進度(publishProgress用到),返回值 類型 @Override protected void onPreExecute() { //第一個執行方法 super.onPreExecute(); } @Override protected String doInBackground(Integer... params) { //第二個執行方法,onPreExecute()執行完后執行 for(int i=0;i<=100;i++){ pb.setProgress(i); publishProgress(i); try { Thread.sleep(params[0]); } catch (InterruptedException e) { e.printStackTrace(); } } return "執行完畢"; } @Override protected void onProgressUpdate(Integer... progress) { //這個函數在doInBackground調用publishProgress時觸發,雖然調用時只有一個參數 //但是這里取到的是一個數組,所以要用progesss[0]來取值 //第n個參數就用progress[n]來取值 tv.setText(progress[0]+"%"); super.onProgressUpdate(progress); } @Override protected void onPostExecute(String result) { //doInBackground返回時觸發,換句話說,就是doInBackground執行完后觸發 //這里的result就是上面doInBackground執行后的返回值,所以這里是"執行完畢" setTitle(result); super.onPostExecute(result); } } } ~~~ [3] 下面看下程序的運行結果截圖: ![](https://box.kancloud.cn/2016-05-16_5739739107019.gif)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看