Android and instances learn how to use AsyncTask, androidasynctask

Source: Internet
Author: User

Android and instances learn how to use AsyncTask, androidasynctask

AsyncTask performs the following four steps:

1. onPreExecute (), run in the UI thread, you can set or modify the UI control, such as displaying a progress bar

2. doInBackground: run in the background thread. You cannot set or modify the UI control. The execution time of this method is as follows:
Call this method immediately after onPreExecute () is executed. You can call the publishProgress method in this method for time-consuming operations.
When the publishProgress method is called, The onProgressUpdate method is triggered immediately.

3. onProgressUpdate, which runs in the UI thread. You can set or modify the UI control.

4. onPostExecute, run in the UI thread. You can set or modify the UI control. This method is completed in the doInBackground method.
It will be called later. The parameter of this method is the result returned by the doInBackground method. You can think like this: doInBackground Method
Since there is a returned result, who is the returned result? The answer is: Pass the parameter form to the onPostExecute Method


Parameter description:
1. The form parameter of the doInBackground method corresponds to the first parameter of AsyncTask, that is, the parameter type must be the same. DoInBackground Method
The time to receive real parameters is: set the value for the execute method when calling the execute method in the UI thread, and call the execute method. This value will be passed to the doInBackground method in the form of parameters, the result returned by the doInBackground method corresponds to the third parameter of AsyncTask, that is, the parameter types must be the same,

2. The parameter value of the onPostExecute method is the result returned by the doInBackground method. Therefore, the parameter of the onPostExecute Method
The type corresponds to the third parameter of AsyncTask. Same

3. The parameter type of the onProgressUpdate method corresponds to the second parameter of AsyncTask, that is, the parameter type must be the same and can be called in the doInBackground method.
The publishProgress method passes the progress value to the onProgressUpdate method. The type of the Progress value is determined by the second parameter type of AsyncTask.

Parameter description summary:
1. The first parameter type of AsyncTask determines the parameter types of the doInBackground method and the execute method.
2. The second parameter type of AsyncTask determines the parameter types of the publishProgress method and onProgressUpdate method.
3. The third parameter type of AsyncTask determines the return value type of the doInBackground method and the parameter type of the onPostExecute method. Note: If no parameter is required, the AsyncTask parameter list can be written as AsyncTask.

Rules that must be followed when the AsyncTask class is used:
1. The AsyncTask class must be loaded in the UI thread.
2. The Task instance must be created in the UI thread.
3.exe cute method must be called in UI thread
4. Do not manually call the onPreExecute, onPostExecute, doInBackground, and onProgressUpdate methods.
5. This Task can only be executed once. Multiple calls will throw an exception.
For details about how to use the AsyncTask class, see the AsyncTask source code comments section.
(Original article: Success !!)

Here is an example: packagecom. text. asynctaskdemo;
Importandroid. app. Activity; importandroid. OS. AsyncTask; importandroid. OS. Bundle; importandroid. util. Log;
PublicclassMainActivity extends Activity {privatestaticfinalString TAG = "asynctaskdemo ";
@ Override protectedvoidonCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); new inclucute ("Test AsyncTask ");}
PrivateclassLoadAllAppsTask extends AsyncTask {@ Override protectedvoidonPreExecute () {// TODO Auto-generated method stub Log. d (TAG, "onPreExecute"); super. onPreExecute ();}
@ Override protectedLong doInBackground (String... params) {// TODO Auto-generated method stub Log. d (TAG, "doInBackground params [0] =" + params [0]); publishProgress (10); return100L ;}
@ Override protectedvoidonProgressUpdate (Integer... values) {// TODO Auto-generated method stub Log. d (TAG, "onProgressUpdate values [0] =" + values [0]); super. onProgressUpdate (values );}
@ Override protectedvoidonPostExecute (Long result) {// TODO Auto-generated method stub Log. d (TAG, "onPostExecute result =" + result); super. onPostExecute (result );}}}
Output result: 06-10 14:17:08. 724: DEBUG/asynctaskdemo (7419): onPreExecute
06-10 14:17:08. 731: DEBUG/asynctaskdemo (7419): doInBackground params [0] = Test AsyncTask
06-10 14:17:08. 771: DEBUG/asynctaskdemo (7419): onProgressUpdate values [0] = 10
06-10 14:17:08. 771: DEBUG/asynctaskdemo (7419): onPostExecute result = 100

DoInBackground (String... the parameter list of the params method can accept multiple parameter values. The following shows how to accept multiple parameter values: input multiple parameters in the 1.exe cute method: newloadallappstask(cmd.exe cute ("Test AsyncTask", "Test AsyncTask2", "Test AsyncTask3"); 2. when obtaining the value of the second parameter in the doInBackground method, you can directly obtain the value of params [1]. If obtaining the third parameter, you can use params [2], as shown below: protectedLong doInBackground (String... params) {// TODO Auto-generated method stub Log. d (TAG, "doInBackground params [1] =" + params [1]); publishProgress (10); return100L;} after modification, run the above program and output the result: 06-10 14:24:57. 834: DEBUG/asynctaskdemo (8269): onPreExecute
06-10 14:24:57. 843: DEBUG/asynctaskdemo (8269): doInBackground params [1] = Test AsyncTask2
06-10 14:24:57. 926: DEBUG/asynctaskdemo (8269): onProgressUpdate values [0] = 10
06-10 14:24:57. 926: DEBUG/asynctaskdemo (8269): onPostExecute result = 100

Related Article

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.