Use of mobile development AsyncTask and mobile development asynctask

Source: Internet
Author: User

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");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.