国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2020-04-11 11:55:01
字體:
來源:轉載
供稿:網友

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

復制代碼 代碼如下:

private class DownloadDBTask extends AsyncTask<String, Integer, String> {  
        // 可變長的輸入參數,與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("第一次使用,正在下載數據...");
            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); // 從網絡上下載數據記錄的功能
            } 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) {   
        } 
     }  

對于寫好的異步任務類,調用方法為:

復制代碼 代碼如下:

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

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

1、onPreExecute(), 在excute調用后立即在ui線程中執行。 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, 在調用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線程中調用. The result of the background computation is passed to this step as a parameter.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 翁源县| 辽中县| 绥芬河市| 勃利县| 长乐市| 公主岭市| 金湖县| 阿瓦提县| 昭觉县| 沂南县| 刚察县| 灌阳县| 赞皇县| 阿瓦提县| 庆元县| 淄博市| 个旧市| 吴忠市| 鞍山市| 视频| 龙陵县| 牟定县| 钦州市| 基隆市| 南宁市| 突泉县| 简阳市| 财经| 盐津县| 永靖县| 嘉峪关市| 手游| 正阳县| 达州市| 新巴尔虎右旗| 当雄县| 崇义县| 怀远县| 罗定市| 大荔县| 汾阳市|