JarvisDownloader
Jarvis is known to those who are familiar with Marvel movies. He is the intelligent steward of Iron Man, helping iron Man to make armor, analyze a large amount of data, assist him in modeling and other intelligent work. Unfortunately, In Avengers ii, Jarvis combined with soul Gem to form Vision, and Iron Man lost the intelligent AI like his family. Later, Tony replaced Jarvis with F.R.I.D.A.Y., but from the interaction between Iron Man and Friday, he only treated Friday as a general AI and didn’t invest in Jarvis as much as he did.
It’s a pity that Avengers iii was not released in China at the same time. I was thinking of watching it in Hong Kong during the May Day, but I didn’t think it would be crowded, so I played Jarvis downloader in my spare time during the holiday. The reason why IT was named after Jarvis is that I like Jarvis so much. It belongs to the top of artificial intelligence ah! Although Jarvis downloader is not as powerful as Jarvis, it has many advantages in downloading files.
The main function
- Support resumable download
- You can customize folder paths and file names.
- Supports customized SQLiteOpenHelper for saving download progress
- Download progress callback automatically switch to the UI thread, easy to update the UI.
- Automatically bind the activity lifecycle without manually releasing it
- The UI is not updated when the activity is not visible, and the state is automatically restored when it is visible
- Query the download history list
- Custom request headers
useJarvisDownloader
Relying on remote libraries
gradle
repositories {
maven{
url "https://jitpack.io"
}
}
dependencies {
api 'com. Making. Yuwenque: JarvisDownloader: 0.4.0'
}
Copy the code
Initializing the database
Jarvis.init(new DefaultDownloadHistoryDBHelper(applicationContext));
Copy the code
You can also customize an inheritance in AbsDownloadHistoryDBHelper management class, used to save the download progress such as operation, details please refer to DefaultDownloadHistoryDBHelper operation
Jarvis.Downloader builds the download task
Call it in your activity or service
// Make up the downloader
Jarvis.Downloader downloader = Jarvis.with(this).withUrl("http://pic1.win4000.com/wallpaper/2017-10-11/59dde2bca944f.jpg");
// Allow the download to continue when the UI is not visible
downloader.allowBackgroundDownload(true);
// Number of multithreaded downloads
downloader.threadCount(3);
/ / set the download directory, not a must, the default directory for the Environment. The external.getexternalstoragedirectory () + File. The separator + "Jarvis,"
downloader.filePath(Environment.getExternalStorageDirectory() + File.separator + "Jarvis");
// Set the file name. Manual setting is recommended. Default is the name of the server file
downloader.fileName("test.jpg");
// The frequency of the refresh progress in milliseconds, with a minimum value of 100
downloader.refreshTime(1000);
// Set the status listener
// If the current context is an activity, Jarvis will automatically call back to the main thread for you, without calling activity.runonuithread
downloader.setDownloadListener(new DownloadListener() {
/** * Callback after file download@param file
*/
@Override
public void onSuccess(File file) {}/** * Schedule callback *@paramDownloadedSize Current downloaded file size *@paramProgress Current progress 0-1.0 */
@Override
public void onProgress(long downloadedSize, float progress) {}/** ** when the download starts */
@Override
public void onStart(a) {}/**
* 下载暂停时
*/
@Override
public void onPause(a) {}/**
* 下载失败时
*/
@Override
public void onFail(a) {}/** ** when deleted@paramB The flag */ is deleted successfully
@Override
public void onDelete(boolean b) {}});// Add additional headers
downloader.addExtraRequestProperty("test-key"."test-value");
// Map can be added when there are multiple request headers
Map<String,String> map = new HashMap<>();
map.put("test1"."value1");
map.put("test2"."value2");
downloader.addExtraRequestPropertyMap(map);
// Start downloading
downloader.download();
// Manual pause
downloader.pause();
// Resume the download
downloader.recovery();
// Manually delete local records but do not delete files. Files cannot be deleted during download
downloader.delete();
// Delete download records and files. Files cannot be deleted during download
downloader.deleteCacheFile();
// Get the current download status
downloader.getDownloadState();
// Obtain the progress of the last download synchronously. It is recommended that a new thread be opened to obtain the progress of the last download
downloader.getDownloadedProgress();
// Get the download progress asynchronously
downloader.getDownloadedProgress(new DataCallBack<Float>() {
@Override
public void onData(Float progress) {}});Copy the code
The above code can also be simplified into the following form
Jarvis.with(this)
.withUrl("http://pic1.win4000.com/wallpaper/2017-10-11/59dde2bca944f.jpg")
.allowBackgroundDownload(true)
.threadCount(3)
.filePath(Environment.getExternalStorageDirectory() + File.separator + "Jarvis")
.fileName("test.jpg")
.refreshTime(1000)
.setDownloadListener(listener).download();
Copy the code
Jarvis manages download tasks
// Obtain download history asynchronously. Please calculate the download progress by yourself
Jarvis.getInstance().getDownloadedList(new DataCallBack<List<LocalFileRecordBean>>() {
@Override
public void onData(List<LocalFileRecordBean> localFileRecordBeans) {
LocalFileRecordBean fileRecordBean= localFileRecordBeans.get(0);
// Download progress
float progress = fileRecordBean.getDownloadedLength()*1.0 f/fileRecordBean.getFileTotalLength(); }});// Stop all downloading tasks
Jarvis.getInstance().pauseAllDownloader();
// Start all download tasks
Jarvis.getInstance().startAllDownload();
// Delete all downloaded tasks and files
Jarvis.getInstance().forceDeleteAll();
// Set the thread pool length for downloading. If there are 10 downloading tasks,
// Each task starts 3 threads for breakpoint download. If you set the number of threads to 20, some download tasks will wait for other threads to complete the task
Jarvis.getInstance().initThreadPoolLength(20);
Copy the code
Source code to read, please jump to https://github.com/yuwenque/JarvisDownloader