Use of mobile development AsyncTask and mobile development asynctask
Handler's code is bloated and the logic is not clear during multi-task operations, so AsyncTask is used.
public abstract class AsyncTask
{}
Params: The parameters passed in when the task is started.
Progress of background task completion
Result: The Result type of background computing.
Private class MyTask extends AsyncTask
{// The onPreExecute method is used to perform some UI operations before executing background tasks @ Override protected void onPreExecute () {}// the doInBackground method internally executes background tasks, you cannot modify the UI @ Override protected String doInBackground (String... params) {// call publishProgress to publish the progress, and the onProgressUpdate method will be executed. The update progress-related UI publishProgress (int) (count/(float) total) * 100); return null;} // The onProgressUpdate method is used to update the progress information @ Override protected void onProgressUpdate (Integer... progresses) {progressBar. setProgress (progresses [0]); textView. setText ("loading... "+ progresses [0] +" % ");} // The onPostExecute method is used to update the UI after the background task is executed. The result @ Override protected void onPostExecute (String result) is displayed) {textView. setText (result);} // The onCancelled method is used to change the UI @ Override protected void onCancelled () {textView when canceling a task in progress. setText ("canceled"); progressBar. setProgress (0 );}}
Call
mTask = new MyTask();mTask.execute("https://www.baidu.com");