Turn from: Android Asynctask full Parse, take you from the source of the perspective of a thorough understanding
Another more detailed blog post: http://blog.csdn.net/liuhe688/article/details/6532519
Basic usage of Asynctask
First look at the basic usage of asynctask, because Asynctask is an abstract class, so if we want to use it, we have to create a subclass to inherit it. At the time of inheritance we can specify three generic parameters for the Asynctask class, and these three parameters are used as follows:
1. Params
Parameters that need to be passed in when executing asynctask can be used in a background task.
2. Progress
When a background task executes, the generic type specified here is used as the progress unit if you need to display the current progress on the interface.
3. Result
When the task finishes executing, if the result needs to be returned, the generic type specified here is used as the return value type.
Therefore, one of the simplest custom asynctask can be written as follows:
1 class extends Asynctask<void, Integer, boolean> {2 ... 3 }
Here we specify the first generic parameter of Asynctask as void, which means that there is no need to pass parameters to the background task when executing asynctask. The second generic parameter is specified as Integer, which indicates that the integer data is used as the progress display unit. The third generic parameter, specified as Boolean, indicates that the execution result is fed back using Boolean data.
Of course, our custom downloadtask is still an empty task and cannot do any actual work, and we need to rewrite several methods in the Asynctask to complete the customization of the task. There are four ways to rewrite the following frequently:
1. OnPreExecute ()
This method is called between the start of the background task and is used for initialization on some interfaces, such as displaying a progress bar dialog box, and so on.
2. Doinbackground (Params ...)
All the code in this method will run in a sub-thread, and we should be here to handle all the time-consuming tasks. Once the task is completed, the execution result of the task can be returned by the return statement, and if Asynctask's third generic parameter specifies void, the task execution result is not returned. Note that the UI action is not allowed in this method, and if you need to update the UI elements, such as feedback on the progress of the current task, you can call Publishprogress (Progress ...). method to complete.
3. Onprogressupdate (Progress ...)
When Publishprogress (Progress ...) is called in a background task Method, the method is quickly called, and the parameters that are carried in the method are passed in the background task. The UI can be manipulated in this method, and the interface elements can be updated accordingly using the values in the parameters.
4. OnPostExecute (Result)
This method is called quickly when the background task is completed and returned through a return statement. The returned data is passed as a parameter to this method, and the returned data can be used to perform some UI actions, such as reminding the result of the task execution, and closing the Progress Bar dialog box.
As a result, a more complete custom asynctask can be written in the following ways:
1 classDownloadtaskextendsAsynctask<void, Integer, boolean> {2 3 @Override4 protected voidOnPreExecute () {5 progressdialog.show ();6 }7 8 @Override9 protectedBoolean doinbackground (Void ... params) {Ten Try { One while(true) { A intDownloadpercent =dodownload (); - publishprogress (downloadpercent); - if(Downloadpercent >= 100) { the Break; - } - } -}Catch(Exception e) { + return false; - } + return true; A } at - @Override - protected voidonprogressupdate (Integer ... values) { -Progressdialog.setmessage ("Current Download progress:" + values[0] + "%"); - } - in @Override - protected voidOnPostExecute (Boolean result) { to Progressdialog.dismiss (); + if(Result) { -Toast.maketext (context, "Download succeeded", Toast.length_short). Show (); the}Else { *Toast.maketext (Context, "Download Failed", Toast.length_short). Show (); $ }Panax Notoginseng } -}
Here we simulate a download task, in the Doinbackground () method to perform the specific download logic, in the Onprogressupdate () method to display the current download progress, in the OnPostExecute () method to prompt the execution of the task results. If you want to start this task, simply call the following code:
1 New Downloadtask (). Execute ();
This is the basic usage of asynctask, how does it feel that switching between a child thread and a UI thread becomes a lot more flexible? We do not need to think about what asynchronous message processing mechanisms are, nor do we have to use a single handler to send and receive messages, just call the Publishprogress () method to easily switch from a child thread to a UI thread.
The above just realized the use of asynctask, the specific implementation mechanism, need to refer to the great god Guo Lin blog. http://blog.csdn.net/guolin_blog/article/details/11711405
Asynchronous message processing mechanism of Android development Asynctask