Detailed explanation of Android multithreaded breakpoint continuous download example

1、 Overview

In the previous blog post "Android - example of multi-threaded download", we explained how to realize the multi-threaded download function of Android. By dividing the whole file into multiple data blocks, starting multiple threads, and allowing each thread to download a corresponding data block respectively, we realized the multi-threaded download function. In multi-threaded downloading, the time-consuming operation of downloading can be executed in the sub thread, which not only does not block the main thread, but also conforms to the design specifications of Android development.

However, in the process of downloading, the mobile phone suddenly gets stuck, or the network is interrupted, and the mobile phone is powered off. At this time, when the mobile phone can be used normally, if you download files again, it doesn't seem to meet the psychological expectations of most users. How to download based on the data downloaded during the last disconnection when the mobile phone can be connected normally? This is called breakpoint download. This article mainly explains how to realize the function of breakpoint download.

The Android breakpoint download explained in this article is based on the previous article "Android - multithreaded download example". This example is based on the previous example. During the download process, the downloaded information is saved to the SQLite database of the Android system. When the mobile phone disconnects the network due to an abnormal situation, the last downloaded data information is recorded in the database, When the mobile phone is connected to the Internet again, read the information in the database and continue to download data from the place where the download was disconnected last time. OK, no more, enter the text.

2、 Server preparation

The implementation of the server is very simple. Here, in order to make the downloaded files larger, I downloaded Youdao dictionary on the network as the test resource to be downloaded. Place it in the webcontent directory of the project and publish the project in the Tomcat server, as shown in the following figure:

In this way, the server is ready. How about it? It's simple, isn't it? I believe everyone will!

3、 Android implementation

The Android implementation part is the focus of this article. Here we start from the layout and explain it slowly from simple to deep. Here we display the program interface through activity, save the downloaded information through SQLite database, operate the saved record information through ContentProvider, and transfer the data in the sub thread to the main process through handler and message mechanism to update the UI display. At the same time, the user-defined listener is used to monitor the UI display update.

1. Layout implementation

The layout is basically the same as that in the previous blog post. There is no major change. A textview is placed from top to bottom on the interface 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. Define the relevant information class downloaddbhelper of the database

In this example, we define the name of the database as download.db. We need to save the primary key ID, the path to be saved after the file is downloaded, the ID of each thread, and the size of the file data block downloaded by each thread. Therefore, there are in the created data table_ ID, path, ThreadID, downloadlength. See the figure below for details

The specific code of downloaddbhelper implementation is as follows:

4. Create downloadprovider class

Downloadprovider class inherits from ContentProvider and provides methods to operate the database. In this class, match the database to be operated through urimatcher class, and obtain a specific database instance through downloaddbhelper object to add, delete, modify and query the corresponding database. The specific implementation is shown in the following code:

5. Create downloadinfo entity class

In order to make the program more object-oriented, we establish downloadinfo entity class to encapsulate the data in the database. The data fields in downloadinfo entity class correspond to the fields in the database. The specific implementation code is as follows:

6. Define the method class downloaddao for external calls to operate the database

Downloaddao class encapsulates a series of methods to operate the database. This class does not directly operate the database object, but calls the methods in downloadprovider through contentresolver object to realize the function of operating the database. Here, contentresolver and ContentProvider, two very important classes in Android, are used. ContentProvider is the content provider, which mainly provides data to the outside. A simple understanding is that an application can provide an interface to operate the application through ContentProvider, and other applications can call the interface provided by ContentProvider to operate the data of the application. Contentresolver is a content receiver, which can receive data provided by ContentProvider. The specific code is as follows:

7. Custom thread class downthread

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. The progress information displayed on the new interface is tracked through the void getdownload (int length) method in the user-defined listener progressbarlistener, At the same time, the download information of data is recorded and updated by calling the downloaddao method. The specific implementation code is as follows:

8. Create a downloaded management class downloadmanager

This class is mainly used to manage the download process, including setting the location of the file to be saved after downloading, calculating the amount of data downloaded by each thread in multi threads, etc. at the same time, it has more recording and updating operations of multi download data than in the article of Android - multi thread Download example. The specific implementation code is as follows:

9. 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, update the interface display, and use the methods in the downloaddao class to record and update the download data. The specific implementation code is as follows:

10. 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

Above: the breakpoint download function in Android is realized. Reminder: you can go to this link to get the complete Android breakpoint download sample source code

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