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.