Asynctask is used in Android to realize file download and progress update prompt

Android provides a tool class: asynctask, which makes it easier to create long-running tasks that need to interact with the user interface. Compared with the handler, asynctask is lighter. It is suitable for simple asynchronous processing and can be implemented without the help of threads and handlers. Asynctask is an abstract class. Asynctask defines three generic types params, progress and result:

Params input parameters for starting task execution, such as URL of HTTP request.

Progress percentage of background task execution.

Result the final result returned by the background execution task, such as string.

By using asynctask to realize the demonstration of file download and progress update prompt:

The download directory of this real machine demonstration is the download folder. First enter the download folder and there are no picture files. After downloading, check again to see the downloaded pictures of this demonstration

First, let's briefly introduce the execution steps of asynctask:

The execution of asynctask is divided into four steps, and each step corresponds to a callback method. What we need is to implement these methods.

(1) First, define a class that inherits asynctask

(2) Implement one or more of the following methods defined in asynctask

The four step methods are:

(1) Onpreexecute(): called by uithread. This method is used to do some preparatory work, such as displaying a progress bar on the interface.

(2) Dolnbackground (params...): will be executed after onpreexecute and run in the background thread. Responsible for performing time-consuming work. You can call the publishprogress method to update the real-time task progress.

(3) Onprogressupdate (Progress...): after the publishprogress method is called, uithread will call this method to display the progress of the task on the interface, for example, through a progress bar.

(4) Onpostexecute (result): after the execution of dolnbackground, onpostexecute method will be called by uithread, and the background calculation result will be passed to uithread through this method.

Effect implementation code example:

Step 1: layout file activity of activity in layout_ main.xml

Step 2: Java implementation code mainactivity.java file

Finally, emphasize the design criteria of asynctask:

(1) An instance of asynctask must be created in ulthread.

(2) the execute method must be invoked in UlThread.

(3) Do not manually call onpreexecute(), onpostexecute (result), dolnbackground (params...) and onprogressupdate (Progress...).

(4) The task can only be executed once, otherwise an exception will occur when it is called multiple times.

(5) Asynctask cannot completely replace threads. Threads may be required to implement some logic that is complex or needs to be executed repeatedly in the background.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>