, The default progress prompt dialog box, namely ProgressDialog, is implemented in Android, and can be used through instantiation and some simple settings.
private class DownloadDBTask extends AsyncTask<String, Integer, String> {
// Variable length input parameters, corresponding to ()
ProgressDialog pdialog;
public DownloadDBTask(Context context){
pdialog = new ProgressDialog(context, 0);
("Cancel", new () {
public void onClick(DialogInterface dialog, int i) {
();
}
});
(new () {
public void onCancel(DialogInterface dialog) {
finish();
}
});
("First use, data downloading...");
(true);
(100);
(ProgressDialog.STYLE_HORIZONTAL);
();
}
@Override
protected String doInBackground(String... params) {
try{
if ((1, mDBHelper).size()==0)
(mDBHelper, pdialog); // Function to download data records from the network
} catch(Exception e) {
();
}
return null;
}
@Override
protected void onCancelled() {
();
}
@Override
protected void onPostExecute(String result) {
();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
}
}
For a written asynchronous task class, the call method is:
DownloadDBTask task = new DownloadDBTask(context);
("");
Note that AsyncTask is a generic class with three generic parameters, which are designed here as <String, Integer, String>, corresponding to the run parameter, progress value type and return parameter.
From the SDK documentation, when an AsyncTask runs, it goes through 4 steps:
1. onPreExecute(), executed in the ui thread immediately after the excute call. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
2. doInBackground, when onPreExecute() is completed, run in the background thread immediately. This step is used to perform background computing that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computing 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, runs in the ui thread after calling publishProgress. The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computing is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
4. onPostExecute, called in the ui thread when the background operation is completed. The result of the background computing is passed to this step as a parameter.