Asynctask executes four steps and executes four methods:
1.onPreExecute (), running on the UI thread, can set or modify UI controls, such as displaying a progress bar
2.doInBackground, running in a background thread, can not set or modify UI controls, the execution time of this method is:
OnPreExecute () calls the method immediately after execution, and takes a time-consuming action in the method, where the Publishprogress method can be called
To publish the progress of execution when the Publishprogress method is called, the Onprogressupdate method is immediately triggered
3.onProgressUpdate, running on the UI thread, can set or modify UI controls
4.onPostExecute, running on the UI thread, can set or modify the UI control, which is done in the Doinbackground method
is called, the parameter of the method is the result returned by the Doinbackground method, you can think: Doinbackground method
Since there is a return result, who does the return result return to? The answer is: Pass the Parameter form to the OnPostExecute method
Parameter description:
The formal parameters of the 1.doInBackground method correspond to the first parameter of Asynctask, which is the same as the parameter type. Doinbackground method
The time to receive an argument is to set a value for the Execute method when the Execute method is called in the UI thread, invoke the Execute method, and pass the parameter to the Doinbackground method. The result returned by the Doinbackground method corresponds to the third parameter of Asynctask, that is, the parameter type is the same,
The parameter value of the 2.onPostExecute method is passed by the result returned by the Doinbackground method. So the parameters of the OnPostExecute method
The type corresponds to the third parameter of the Asynctask. That is the same
The parameter type of the 3.onProgressUpdate method corresponds to the second parameter of Asynctask, which is the same as the parameter type, which can be called in the Doinbackground method.
The Publishprogress method passes a progress value to the Onprogressupdate method, and the type of the progress value is determined by the second parameter type of Asynctask
Summary of parameter Description:
The first parameter type of 1.AsyncTask determines the Doinbackground method, the parameter type of the Execute method
The second parameter type of 2.AsyncTask determines the Publishprogress method, the parameter type of the Onprogressupdate method
The third parameter type of 3.AsyncTask determines the return value type of the Doinbackground method, the parameter type of the OnPostExecute method Note: If no parameter passing is required, then the Asynctask argument list can be written as: Asynctask
Rules that you must follow to use the Asynctask class:
The 1.AsyncTask class must be loaded in the UI thread
2.Task instances must be created in the UI thread
The 3.execute method must be called in the UI thread
4. Do not manually invoke the OnPreExecute, OnPostExecute, Doinbackground, Onprogressupdate methods
5. The task can only be executed once, and multiple calls will throw an exception
The use of the Asynctask class can be found in the Asynctask source comments section, which contains detailed instructions
(The original link: http://blog.csdn.net/yelangjueqi/article/details/38322647, welcome reprint, please specify the source when reproduced!!) )
Show 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 Loadallappstask (). Execute ("Test asynctask"); }
Privateclassloadallappstask extends Asynctask {@Override Protectedvoidonpreexecute () {//TODO auto-generated method Stu b log.d (TAG, "OnPreExecute"); Super.onpreexecute (); }
@Override protectedlong doinbackground (String ... params) {//TODO auto-generated Method Stub log.d (TAG, "Doinbackground p Arams[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: 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
The formal parameter list of the Doinbackground (String ... params) method can accept multiple parameter values, and the procedure for accepting multiple parameter values is shown below: The 1.execute method passes multiple parameters: Newloadallappstask (). Execute ("Test Asynctask", "Test AsyncTask2", "Test AsyncTask3"); 2.doInBackground method to get the value of the second parameter, the direct PARAMS[1] can be taken, get the third to use params[2], as follows: Protectedlong Doinbackground (String ... params) {//TODO auto-generated Method Stub log.d (TAG, "Doinbackground params[1]=" + params[1]); publishprogress; Retu rn100l; After the modification, run the above program, output: 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
Android combined with examples learn how to use Asynctask