android教程之使用asynctask在后臺運行耗時任務(wù)
, Android中實現(xiàn)了默認的進度提示對話框,即ProgressDialog,通過實例化和一些簡單設(shè)置,就可以使用了。
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)用方法為:
DownloadDBTask task = new DownloadDBTask(context);
task.execute("");
注意AsyncTask為泛型類,具有三個泛型參數(shù),此處設(shè)計為 <String, Integer, String>,對應(yīng)于運行參數(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.
相關(guān)文章
Android判斷網(wǎng)絡(luò)狀態(tài)的代碼
這篇文章主要為大家詳細介紹了Android判斷網(wǎng)絡(luò)狀態(tài)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
這篇文章主要給大家介紹了關(guān)于利用Android如何仿網(wǎng)易新聞圖片詳情下滑隱藏效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android編程實現(xiàn)在Activity中操作刷新另外一個Activity數(shù)據(jù)列表的方法
這篇文章主要介紹了Android編程實現(xiàn)在Activity中操作刷新另外一個Activity數(shù)據(jù)列表的方法,結(jié)合具體實例形式分析了2種常用的Activity交互實現(xiàn)技巧,需要的朋友可以參考下2017-06-06
Android RetainFragment狀態(tài)保存的方法
本篇文章主要介紹了Android RetainFragment狀態(tài)保存的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Android實現(xiàn)帶有指示器的自定義底部導(dǎo)航欄
這篇文章主要為大家詳細介紹了Android實現(xiàn)帶有指示器的自定義底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
android隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實例
下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Android調(diào)試出現(xiàn)The selected device is incompatible問題解決
這篇文章主要介紹了Android調(diào)試出現(xiàn)The selected device is incompatible問題解決的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android倒計時控件 Splash界面5秒自動跳轉(zhuǎn)
這篇文章主要為大家詳細介紹了Android倒計時控件,Splash界面5秒自動跳轉(zhuǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09

