[Android instance] resumable download for Android, android instance

Source: Internet
Author: User

[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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.