[Android instance] resumable download for Android, android instance
What we often encounter during development is download. There are many download methods, so how can we achieve resumable download! Many people have a headache. If we do not have good logic, it is really not easy to solve. I have sorted out a project based on my previous documents to download multiple files at the same time.
Resumable download, as its name implies, is to exit the download before a download is completed. The second download will continue with the download progress. So how can we record the data downloaded for the first time? The database must be used here. The following is a SQLiteOpenHelper class for creating a database. Used to create a database at the first run.
DBHelper. java
Copy code
The layout of the main interface is shown below. Here, I only designed a ListView to display the name of the downloaded music, a start Download button, and a pause button.
The layout file is as follows:
Main. xml:
Copy code
List_item.xml:
Copy code
We want to define a class for recording the data of each period during download. Here we create a DownloadInfo class to record. The Code is as follows:
DownloadInfo:
Copy code
During the download, we have a progress bar to display the progress. How can we determine the progress, size, and start position of the progress bar? Here I have defined a LoadInfo class to record the detailed information of the downloader. The Code is as follows:
LoadInfo:
Copy code
The following is the most important step, which is to define a download tool for download. I will not talk about it here. The specific explanations are annotated in the code for your reference.
Downloader:
Copy code
In the definition of the download class here, we have used many methods for database operations. Here I have defined a database tool class to provide these methods. The Code is as follows:
Dao:
Copy code
The following is the main program of the program. If you don't talk about anything else, go directly to the Code:
Copy code
At last, we need to add these two permissions in android Manifest. xml: internet access and external write permission.
Copy code
Last run:
Everything is ready! Haha, I need to put a few music files in my web project.
The downloaded file can be found under sdcard!