The android tutorial USES asynctask to run time consuming tasks in the background

  • 2020-05-24 06:08:06
  • OfStack

The default progress prompt dialog box, ProgressDialog, is implemented in Android and can be used by instantiating and setting some simple Settings.


private class DownloadDBTask extends AsyncTask<String, Integer, String> {   
        //  Variable length input parameters, and AsyncTask.exucute() The corresponding    
        ProgressDialog pdialog;   
        public DownloadDBTask(Context context){   
            pdialog = new ProgressDialog(context, 0);      
            pdialog.setButton(" cancel ", 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(" The first 1 This time, downloading data ...");
            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); //  The ability to download data records from the network 
            } 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) {    
        }  
     }   

For the written asynchronous task class, the call method is:


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

Note that AsyncTask is a generic class with three generic parameters, designed here as < String, Integer, String > , corresponding to the run parameter, the progress value type, and the return parameter.
As you can see from the sdk documentation, when an AsyncTask is running, there are four steps:

1. onPreExecute(), which is executed in the ui thread immediately after the excute call. This step is normally used to setup the task, for instance by a progress bar in the user interface
2. doInBackground. When onPreExecute() is finished, This step is used to computation that that time The parameters of asynchronous task are passed this this this The result of the computation

by this

or more units of progress. These values published on the UI thread, in the onProgressUpdate step.
3. onProgressUpdate. After calling publishProgress, The timing the execution is undefined This method display of in the user while the background computation still executing. For instance, it can be bar a text field
onPostExecute is called in the ui thread when the background operation is completed.


Related articles: