Reference URL: http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html
1 /*2 * Params starts the input parameters of the task execution, such as the URL of the HTTP request. 3 * Percentage of Progress background task execution. 4 * Result background performs the final return of the task, such as String5 * 6 * Doinbackground (Params ...): Background execution, more time-consuming operations can be placed here. Note that it is not possible to manipulate the UI directly. 7 * This method is executed in the background thread, which usually takes a long time to complete the task's main work. 8 * publicprogress (Progress ...) can be called during execution. To update the progress of the task. 9 * OnPostExecute (Result): equivalent to the way handler handles the UI, where the result processing operation UI can be used in Doinbackground. Ten * This method is executed on the main thread, and the result of the task execution is returned as a parameter of this method One * A * If necessary, you have to rewrite the following three methods, but not required: - * Onprogressupdate (Progress ...) You can use the progress bar to increase user experience. This method executes on the main thread and is used to show the progress of the task execution. - * OnPreExecute () Here is the interface when the end user calls Excute, and the progress dialog box can be displayed here before the task executes before calling this method. the * oncancelled () The action to be made when the user calls cancel - */ - Public classMyasynctaskextendsAsynctask<string, Integer, list<product>> - { + @Override - protected voidOnPreExecute () + { AToast.maketext (Getapplicationcontext (), "Start loading", Toast.length_short). Show (); at Super. OnPreExecute (); - } - - //* Background Run time-consuming operation, can not directly manipulate the data inside the main UI thread, through functions publishprogress and onprogressupdate indirectly manipulate the data inside the main UI thread - @Override - protectedList<product>doinbackground (String ... params) in { - //get a list of products that cannot manipulate data within the main UI thread toList<product> temproducts =NewGetproductservice (). getproduct (page); + //For (Integer i = 0; i < 3; ++i) - // { the //publishprogress (i); //notifies onprogressupdate by calling this function and can modify parameters in the main thread of the UI in Onprogressupdate * // } $ returntemproducts;Panax Notoginseng } - the //* Call the publishprogress function in a child thread to notify this function to manipulate the data in the main UI thread (mainly the progress bar data) + @Override A protected voidonprogressupdate (Integer ... values) the { + intVlaue = Values[0];//gets the progress value passed by the calling Publisprogress in the main thread. -Toast.maketext (Getapplicationcontext (), "Loading progress:" +Vlaue, Toast.length_long). Show (); $ Super. Onprogressupdate (values); $ } - - //This is equivalent to the way handler handles the UI, where you can use the results that you get in Doinbackground to manipulate the UI. This method is executed on the main thread, and the result of the task execution is returned as a parameter to this method the //the product parameter here corresponds to the third parameter in Asynctask ( that is, the return value of the receiving Doinbackground) - //The UI space can be set by running after the Doinbackground method execution and running in the UI threadWuyi @Override the protected voidOnPostExecute (list<product>product) - { WuToast.maketext (Getapplicationcontext (), "Load Complete", Toast.length_long). Show (); -Listutil.products = Products;//Get Product List Object AboutIsLoading =false;//set load Flag status bit: Load Complete $ - adapter.setproducts (product); - adapter.notifydatasetchanged (); - if(product.size () = = 0 ) A { +Toast.maketext (Getapplicationcontext (), "finished loading", Toast.length_long). Show (); the Productlistview.removefooterview (footview); -IsLoading =true; $ } the } the the @Override the protected voidOncancelled (list<product>result) - { in Super. oncancelled (result); the } the}
2015.01.15 (Android Asynctask)