a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

android教程之使用asynctask在后臺運行耗時任務

2020-04-11 11:55:01
字體:
供稿:網(wǎng)友

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

復制代碼 代碼如下:

private class DownloadDBTask extends AsyncTask<String, Integer, String> {  
        // 可變長的輸入?yún)?shù),與AsyncTask.exucute()對應  
        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)絡上下載數(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) {   
        } 
     }  

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

復制代碼 代碼如下:

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

注意AsyncTask為泛型類,具有三個泛型參數(shù),此處設計為 <String, Integer, String>,對應于運行參數(shù)、進度值類型和返回參數(shù)。
從sdk的文檔中看到,當一個AsyncTask運行的過程中,經(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, 當 onPreExecute() 完成后, 立即在后臺線程中運行. 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線程中運行. 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, 后臺運算完成時在ui線程中調(diào)用. The result of the background computation is passed to this step as a parameter.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧洲精品一区二区 | 色啪网| 天天曰| 蜜臀av在线播放一区二区三区 | 精品久久久影院 | 亚洲一区二区精品视频 | 国产精品久久久久久亚洲调教 | 日韩h在线观看 | 欧美精品一区二区在线观看 | 久久久国产视频 | 亚洲午夜精品一区二区三区他趣 | 午夜精品福利一区二区三区蜜桃 | 久久久精品国产 | 日本三级在线视频 | 欧美自拍视频一区 | 国产精品久久久久久久久久久久 | 9999国产精品欧美久久久久久 | 亚洲成人一区二区三区 | 欧美国产精品一区 | 国产一二区在线 | 狠狠狠干| japan护士性xxxⅹhd | 免费的黄网 | 国产乱码精品一区二区三区手机版 | www精品美女久久久tv | 国产不卡区 | 自拍视频网 | 免费看片色 | 久久草视频 | 欧美成人激情视频 | 一本色道久久综合狠狠躁篇怎么玩 | 国产精品视频播放 | 色噜噜精品 | 国产a免费 | 成人在线精品视频 | 成人三级影院 | 91免费看电影 | 三级av | 成人免费视频国产免费麻豆 | 青青草免费在线观看 | 免费看男女www网站入口在线 |