欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android教程之使用asynctask在后臺運(yùn)行耗時任務(wù)

 更新時間:2014年02月14日 12:53:06   作者:  
AsyncTask用在需要在ui線程中調(diào)用、在背景線程中執(zhí)行耗時任務(wù)、并且在ui線程中返回結(jié)果的場合。下面就是一個在背景中運(yùn)行的AsyncTask的實現(xiàn)DownloadDBTask

, Android中實現(xiàn)了默認(rèn)的進(jìn)度提示對話框,即ProgressDialog,通過實例化和一些簡單設(shè)置,就可以使用了。

復(fù)制代碼 代碼如下:

private class DownloadDBTask extends AsyncTask<String, Integer, String> {  
        // 可變長的輸入?yún)?shù),與AsyncTask.exucute()對應(yīng)  
        ProgressDialog pdialog;  
        public DownloadDBTask(Context context){  
            pdialog = new ProgressDialog(context, 0);     
            pdialog.setButton("取消", new DialogInterface.OnClickListener() {  
             public void onClick(DialogInterface dialog, int i) {  
              dialog.cancel();  
             }  
            });  
            pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {  
             public void onCancel(DialogInterface dialog) {  
              finish();  
             }  
            });
            pdialog.setTitle("第一次使用,正在下載數(shù)據(jù)...");
            pdialog.setCancelable(true);  
            pdialog.setMax(100);  
            pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
            pdialog.show();  
        }

        @Override 
        protected String doInBackground(String... params) {  
            try{
                    if (DataOper.GetTopNearestPOIs(1, mDBHelper).size()==0)
                            DataOper.GetAllPtsFromNet(mDBHelper, pdialog); // 從網(wǎng)絡(luò)上下載數(shù)據(jù)記錄的功能
            } catch(Exception e) {  
                    e.printStackTrace();
            }  
            return null;
        }

        @Override 
        protected void onCancelled() {  
            super.onCancelled();  
        }  

        @Override 
        protected void onPostExecute(String result) {  
            pdialog.dismiss();   
        }  

        @Override 
        protected void onPreExecute() {
        }  

        @Override 
        protected void onProgressUpdate(Integer... values) {   
        } 
     }  

對于寫好的異步任務(wù)類,調(diào)用方法為:

復(fù)制代碼 代碼如下:

DownloadDBTask task = new DownloadDBTask(context);  
task.execute("");

注意AsyncTask為泛型類,具有三個泛型參數(shù),此處設(shè)計為 <String, Integer, String>,對應(yīng)于運(yùn)行參數(shù)、進(jìn)度值類型和返回參數(shù)。
從sdk的文檔中看到,當(dāng)一個AsyncTask運(yùn)行的過程中,經(jīng)歷了4個步驟:

1、onPreExecute(), 在excute調(diào)用后立即在ui線程中執(zhí)行。 This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
2、doInBackground, 當(dāng) onPreExecute() 完成后, 立即在后臺線程中運(yùn)行. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate step.
3、onProgressUpdate, 在調(diào)用publishProgress后,在ui線程中運(yùn)行. The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
4、onPostExecute, 后臺運(yùn)算完成時在ui線程中調(diào)用. The result of the background computation is passed to this step as a parameter.

相關(guān)文章

  • Android?搜索框架使用詳解

    Android?搜索框架使用詳解

    這篇文章主要為大家介紹了Android?搜索框架使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • android使用handlerthread創(chuàng)建線程示例

    android使用handlerthread創(chuàng)建線程示例

    這篇文章主要介紹了android使用handlerthread創(chuàng)建線程,講解了這種方式的好處及為什么不使用Thread類的原因
    2014-01-01
  • Android Framework Application Framework層簡單介紹

    Android Framework Application Framework層簡單介紹

    這篇文章主要介紹了 Android Framework Application Framework層簡單介紹的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android 歡迎全屏圖片詳解及實例代碼

    Android 歡迎全屏圖片詳解及實例代碼

    這篇文章主要介紹了Android 歡迎全屏圖片詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android 通過API獲取數(shù)據(jù)庫中的圖片文件方式

    Android 通過API獲取數(shù)據(jù)庫中的圖片文件方式

    這篇文章主要介紹了Android 通過API獲取數(shù)據(jù)庫中的圖片文件方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Kotlin基礎(chǔ)學(xué)習(xí)之循環(huán)和異常

    Kotlin基礎(chǔ)學(xué)習(xí)之循環(huán)和異常

    最近在學(xué)習(xí)kotlin,Kotlin 是一個基于 JVM 的新的編程語言,下面這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之循環(huán)和異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • android實現(xiàn)長圖加載效果

    android實現(xiàn)長圖加載效果

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)長圖加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 解析Kotlin?JSON格式

    解析Kotlin?JSON格式

    這篇文章主要介紹了Kotlin?JSON格式解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL

    Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL

    這篇文章主要為大家介紹了Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android二級緩存加載圖片實現(xiàn)照片墻功能

    Android二級緩存加載圖片實現(xiàn)照片墻功能

    這篇文章主要為大家詳細(xì)介紹了Android二級緩存加載圖片實現(xiàn)照片墻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論