1. First write a class inheriting asynctask such as: Class Downloadtask extends Asynctask<parms, Process, Result>
Parms is the execution of Asynctask incoming parameters, such as: Task.execute ("First parameter", "Second parameter", "ETC.") Such as:
asynctask<string,void,void> task = new asynctask<string,void,void>public Void doinbackground (String ... params) {for (String parameter:params) {log.i (TAG, "Received parameter:" + parameter); } return null; }};
Process is the display of the current progress, such as
public void Onprogressupdate (Integer ... params) {int progress = params[0]; Gestationprogressbar.setprogress (progress);}
Result is the return of the result when the task has finished executing. For example:
Private class Fetchitemstask extends asynctask<void,void,list<galleryitem>>{ @Override Protected list<galleryitem> doinbackground (Void ... params) { return new FLICKRFETCHR (). Fetchitems (); }}
2. To replicate the methods as needed, there are several ways to do this:
OnPreExecute () This method is called before the background task begins execution, and is used for initialization of some interfaces, such as displaying a progress bar dialog box, and so on.
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. If you need to update UI elements, you can call Publishprogress (Progress ...). method to complete.
Onprogressupdate (Progress ...) When Publishprogress (Progress ...) is called in a background task Method, the method is called quickly, and the parameters that are carried in the method are passed in the background task.
OnPostExecute (Result) This method is called quickly when a background task is completed and returned through a return statement. The returned data is passed as a parameter to this method, and some UI actions can be made with the returned data.
3. If you want to start this task, simply write the following code: New Downloadtask (). Execute ();
Basic use of Android Asynctask