Android multi-thread resumable download

Source: Internet
Author: User

Multi-thread breakpoint download flowchart:

Introduction to multi‑thread resumable download principles:

When downloading, multiple threads can occupy more resources on the server to speed up the download.

When downloading data on the mobile phone end, there may inevitably be no signal disconnection or insufficient power. Therefore, the resumable data transfer function is required.

Calculate the location of Data downloaded by each thread based on the length of the downloaded data. Multiple Threads are enabled for concurrent download in the program.

You can set the range field in the request header to obtain data at the specified position, for example, range: bytes = 100-200.

Record the number of data copies in each thread during the download process. If the download is interrupted, download continues from the recorded position at the next startup.

Instance diagram:

1. Use <progress> to configure the progress bar

1.1 The default is a circular progress bar. You must configure the style attribute for the horizontal progress bar. Android: ATTR/progressbarstylehorizontal

Use Android. R. ATTR. progressbarstylehorizontal as the style

1.2 enable multi-threaded download when you click the Download button. Modify the progress bar during the download process.

Set the maximum scale: setmax ()

Set the current progress: setprogress ()


Ii. resumable upload

2.1 resumable download requires that the download progress of each thread be recorded during the download process (stored in the file name. Temp File is the same as the. Temp File function in thunder)

2.2 read the database before each download starts. check whether there are any unfinished records and continue the download. If not, create a new record and insert it into the database.

2.3 update the download progress in the database after each write to the file

2.4 After the download is complete, delete the download record (file name. Temp File) in the database)

3. Handler transfers data

3.1 The view created in the main thread can only be modified in the main thread. Other threads can only communicate with the main thread and change the view data in the main thread.

3.2 handler can be used to deal with such requirements

3.3 create a handler in the main thread and override the handlemessage () method

3.4 When a handler is used to send a message in the new thread, the main thread can receive the message and execute the handlemessage () method.

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.