An example of multithreaded breakpoint continuous transmission downloader developed by Android programming

This paper describes the example of Android programming and implementation of multi-threaded breakpoint continuous download device. Share with you for your reference, as follows:

Using the multithreaded breakpoint retransmission Downloader, when downloading, multiple threads are concurrent, which can occupy more resources on the server side, so as to speed up the download speed. During the download process, record the amount of data copied by each thread. If the download is interrupted, such as no signal disconnection, insufficient power, etc., the breakpoint retransmission function needs to be used, Continue downloading from the recording location at the next startup to avoid repeated downloading. Here, a database is used to record the progress of the download.

design sketch:

Breakpoint continuation

1. Breakpoint continuation needs to record the download progress of each thread during the download process. 2. Before each download, read the database to query whether there are unfinished records. If there are, continue to download. If not, create a new record and insert it into the database. 3. Update the download progress in the database after writing data to the file every time. 4. Delete the download records in the database after the download is completed

Handler transfer data

This is mainly used to record the percentage. Every time a part of the data is downloaded, the main thread is notified to record the time

1. The view created in the main thread can only be modified in the main thread. Other threads can only change the view data in the main thread by communicating with the main thread. 2. We can use handler to handle this requirement

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

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

Dynamically generate new view

Multi task download

1. Create an XML file and configure the view to be generated. 2. Obtain the system service layoutinflator to generate a new view

Example

Progress bar style download.xml

Top style main.xml

MainActivity.java

Downloader.java

Dao:

DBOpenHelper:

InfoDao:

I hope this article will help you in Android programming.

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