Detailed explanation of Android multithreaded download example

1、 Overview

When it comes to file downloading in Android, the Android API clearly requires that time-consuming operations be executed in a sub thread. File downloading undoubtedly takes time, so file downloading should be executed in a sub thread. Next, let's implement a small example of downloading files using multithreading in Android.

2、 Server preparation

In this small example, I take downloading Youdao dictionary as an example, download the installation package of Youdao dictionary on the Internet, create a new web project in eclipse, place the downloaded installation package of Youdao dictionary in the webcontent directory, and publish the project to tomcat, as shown in the following figure

3、 Android implementation

1. Layout

A textview is placed on the interface from top to bottom to prompt the information entered in the text box. A text box is used to enter the path of downloading files in the network. A button is used to click the download file. A ProgressBar displays the download progress and a textview displays the percentage of downloads. The specific layout is as follows:

2. Customize progressbarristener listener interface

Create a new custom progressbarlistener listener interface. Two methods are defined in this interface. Void getmax (int length) is used to obtain the length of the downloaded file, and void getdownload (int length); It is used to get the length of each download. This method is mainly called in multithreading. The data obtained in the sub thread is passed to these two interface methods, and then the corresponding length information is passed to the main thread through Handler in the two interface methods, and the update interface displays information. The specific code is as follows:

3. Custom thread class downloadthread

Here, the user-defined thread operation is realized by inheriting the thread. In this class, the file download operation is mainly realized. In this class, a series of instance variables related to download are defined to control the downloaded data. At the same time, the progress information displayed on the new interface is tracked through the void getdownload (int length) method in the user-defined listener progressbarlistener. The specific implementation is as follows:

4. Create a new downloadmanager class

This class is mainly used to manage the download process, including downloading, setting the location of the file to be saved after downloading, calculating the amount of data downloaded by each thread in multithreading, and so on. The specific implementation is as follows:

5. Improve mainactivity

In this class, first, find each control in the page, implement the onclick event of the button button, start a thread in the onclick event for download operation, and transfer the data obtained from the sub thread to the main thread through the handler and message mechanism to update the interface display. The specific implementation is as follows:

6. Add permissions

Finally, don't forget to authorize the application. Here we need to use Android networking authorization and the permission to write files to SD card. The specific implementation is as follows:

4、 Operation effect

Reminder: you can go to this link to get the complete code example.

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
分享
二维码
< <上一篇
下一篇>>