├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── lwc │ │ └── simpledownload │ │ ├── FirstActivity.java │ │ └── MainActivity.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_first.xml │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── download ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── lwc │ │ └── download │ │ ├── DownState.java │ │ ├── DownloadInfo.java │ │ ├── DownloadInterceptor.java │ │ ├── DownloadListener.java │ │ ├── DownloadManager.java │ │ ├── DownloadProgressListener.java │ │ ├── DownloadResponse.java │ │ ├── DownloadService.java │ │ └── FileUtils.java │ └── res │ └── values │ └── strings.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleDownload 2 | 简介:Retrofit+OkHttp+RxJava实现的下载工具类 3 | 4 | ### 一、本框架实现的功能特性 5 | 本下载框架实现了这样的特性: 6 | 7 | 1. 支持断点续传。即下载断开后,支持从已完成的部分开始继续下载。 8 | 2. 支持多个任务同时下载。 9 | 3. 有各种情况的回调。如下载进度回调,任务取消、完成等回调。 10 | 4. 下载任务在子线程进行,回调在主线程。不需要用户自己切换线程。 11 | 5. 用户可以自定义文件保存路径和文件名。 12 | 13 | 14 | ### 二、最终效果图 15 | 16 | 17 | ### 三、使用方法 18 | 如果想在Android Studio项目中使用本框架,下面是使用步骤: 19 | 20 | 1.项目根目录下的Build.gradle文件中修改: 21 | 22 | ``` 23 | allprojects { 24 | repositories { 25 | google() 26 | jcenter() 27 | maven { url 'https://www.jitpack.io' } // 添加这行 28 | } 29 | } 30 | ``` 31 | 2.模块Build.gradle文件添加: 32 | 33 | ``` 34 | dependencies { 35 | implementation fileTree(dir: 'libs', include: ['*.jar']) 36 | api 'com.github.liwuchen:SimpleDownload:1.4.0' // 添加这行 37 | } 38 | ``` 39 | 请使用最新版本。 40 | 41 | 3.添加权限(**读写文件的权限还需动态申请**,这里就不贴出了): 42 | 43 | ``` 44 | 45 | 46 | 47 | ``` 48 | 49 | 4.代码中使用起来也很简单: 50 | 51 | ```java 52 | // 自定义文件夹名 53 | private final String FOLDER_NAME = "SimpleDownload"; 54 | // 文件保存路径 55 | private final String SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + FOLDER_NAME; 56 | // 文件下载地址 57 | private final String url = "文件下载地址"; 58 | // 保存文件名 59 | private final String fileName = "1.file"; 60 | ``` 61 | 62 | ```java 63 | // 定义下载回调 64 | DownloadListener listener = new DownloadListener() { 65 | @Override 66 | public void onStartDownload(TextView textView) { 67 | Log.d(TAG, "1 onStartDownload() called"); 68 | } 69 | 70 | @Override 71 | public void onProgress(long downloaded, long total, TextView textView) { 72 | Log.d(TAG, "1 onProgress() called with: downloaded = [" + downloaded + "], total = [" + total + "]"); 73 | textView.setText(getPercentString(downloaded, total)); 74 | } 75 | 76 | @Override 77 | public void onPauseDownload(TextView textView) { 78 | Log.d(TAG, "1 onPauseDownload() called"); 79 | textView.setText("暂停"); 80 | } 81 | 82 | @Override 83 | public void onCancelDownload(TextView textView) { 84 | Log.d(TAG, "1 onCancelDownload() called"); 85 | textView.setText("已取消"); 86 | } 87 | 88 | @Override 89 | public void onFinishDownload(String file, TextView textView) { 90 | Log.d(TAG, "1 onFinishDownload() called:" + file); 91 | textView.setText("已完成"); 92 | } 93 | 94 | @Override 95 | public void onFail(String errorInfo, TextView textView) { 96 | Log.d(TAG, "1 onFail() called with: errorInfo = [" + errorInfo + "]"); 97 | textView.setText("下载出错"); 98 | } 99 | }; 100 | ``` 101 | 102 | ```java 103 | DownloadManager downloadManager = DownloadManager.getInstance(); 104 | // 开始下载 105 | TextView tvStatus = findViewById(R.id.tvStatus); 106 | downloadManager.download(url, SAVE_PATH, fileName, tvStatus, listener); 107 | // 暂停下载 108 | downloadManager.pauseDownload(url); 109 | // 继续下载 110 | downloadManager.continueDownload(url); 111 | // 取消下载,不删除文件 112 | downloadManager.cancelDownload(url, false); 113 | // 取消下载,并删除文件 114 | downloadManager.cancelDownload(url, true); 115 | ``` 116 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.4.0' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | maven { url 'https://www.jitpack.io' } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.0" 6 | defaultConfig { 7 | applicationId "com.lwc.simpledownload" 8 | minSdkVersion 19 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'androidx.appcompat:appcompat:1.1.0' 25 | api 'com.github.liwuchen:SimpleDownload:1.4.1' 26 | // api project(":download") 27 | } 28 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo/src/main/java/com/lwc/simpledownload/FirstActivity.java: -------------------------------------------------------------------------------- 1 | package com.lwc.simpledownload; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import androidx.annotation.Nullable; 9 | 10 | public class FirstActivity extends Activity { 11 | @Override 12 | protected void onCreate(@Nullable Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_first); 15 | } 16 | 17 | public void goToMainPage(View view) { 18 | startActivity(new Intent(this, MainActivity.class)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /demo/src/main/java/com/lwc/simpledownload/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lwc.simpledownload; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.appcompat.app.AppCompatActivity; 5 | 6 | import android.Manifest; 7 | import android.content.pm.PackageManager; 8 | import android.os.Build; 9 | import android.os.Bundle; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.TextView; 13 | import android.widget.Toast; 14 | 15 | import com.lwc.download.DownState; 16 | import com.lwc.download.DownloadListener; 17 | import com.lwc.download.DownloadManager; 18 | 19 | import java.io.File; 20 | 21 | public class MainActivity extends AppCompatActivity { 22 | private static final String TAG = "Download"; 23 | public static final int REQUEST_CODE = 100; 24 | 25 | private static TextView tvProgress1; 26 | private static TextView tvProgress2; 27 | private DownloadManager downloadManager = DownloadManager.getInstance(); 28 | private String permissions[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; 29 | private final String FOLDER_NAME = "SimpleDownload"; 30 | private String savePath; 31 | 32 | private final String url1 = "http://ccr.csslcloud.net/5D2636511DBBCADD/BBD5D1D6504FF2AD9C33DC5901307461/8DE588DAEE2FE914.ccr"; 33 | private final String fileName1 = "1.file"; 34 | private final String url2 = "http://ccr.csslcloud.net/5D2636511DBBCADD/BBD5D1D6504FF2AD9C33DC5901307461/D030B9064D6442EB.ccr"; 35 | private final String fileName2 = "2.file"; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_main); 41 | tvProgress1 = findViewById(R.id.tvProgress1); 42 | tvProgress2 = findViewById(R.id.tvProgress2); 43 | 44 | setOnClickListeners(); 45 | requestPermissions(); 46 | savePath = getApplication().getExternalCacheDir() + File.separator + FOLDER_NAME; 47 | 48 | restoreDownState(url1, tvProgress1); 49 | restoreDownState(url2, tvProgress2); 50 | } 51 | 52 | private void restoreDownState(String url, TextView textView) { 53 | if (DownloadManager.getInstance().getTaskState(url) == DownState.DOWNLOADING) { 54 | DownloadManager.getInstance().updateStatusTextView(url, textView); 55 | } else if (DownloadManager.getInstance().getTaskState(url) == DownState.PAUSE) { 56 | DownloadManager.getInstance().updateStatusTextView(url, textView); 57 | textView.setText("暂停"); 58 | } 59 | } 60 | 61 | private void requestPermissions() { 62 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 63 | requestPermissions(permissions, REQUEST_CODE); 64 | } 65 | } 66 | 67 | @Override 68 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 69 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 70 | if (requestCode == REQUEST_CODE ) { 71 | if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { 72 | Toast.makeText(this, "没有权限保存文件", Toast.LENGTH_LONG).show(); 73 | finish(); 74 | } 75 | } 76 | } 77 | 78 | private void setOnClickListeners() { 79 | 80 | // 第一组 81 | findViewById(R.id.btnStart1).setOnClickListener(new View.OnClickListener() { 82 | @Override 83 | public void onClick(View v) { 84 | downloadManager.download(MainActivity.this, url1, savePath, fileName1, tvProgress1, listener1); 85 | } 86 | }); 87 | 88 | findViewById(R.id.btnStop1).setOnClickListener(new View.OnClickListener() { 89 | @Override 90 | public void onClick(View v) { 91 | downloadManager.pauseDownload(url1); 92 | } 93 | }); 94 | 95 | findViewById(R.id.btnContinue1).setOnClickListener(new View.OnClickListener() { 96 | @Override 97 | public void onClick(View v) { 98 | downloadManager.continueDownload(url1); 99 | // downloadManager.continueDownload(url1, listener1); 100 | } 101 | }); 102 | 103 | findViewById(R.id.btnRemove1).setOnClickListener(new View.OnClickListener() { 104 | @Override 105 | public void onClick(View v) { 106 | downloadManager.cancelDownload(url1, false); 107 | } 108 | }); 109 | 110 | 111 | // 第二组 112 | findViewById(R.id.btnStart2).setOnClickListener(new View.OnClickListener() { 113 | @Override 114 | public void onClick(View v) { 115 | downloadManager.download(MainActivity.this, url2, savePath, fileName2, listener2); 116 | } 117 | }); 118 | 119 | findViewById(R.id.btnStop2).setOnClickListener(new View.OnClickListener() { 120 | @Override 121 | public void onClick(View v) { 122 | downloadManager.pauseDownload(url2); 123 | } 124 | }); 125 | 126 | findViewById(R.id.btnContinue2).setOnClickListener(new View.OnClickListener() { 127 | @Override 128 | public void onClick(View v) { 129 | downloadManager.continueDownload(url2, listener2); 130 | } 131 | }); 132 | 133 | findViewById(R.id.btnRemove2).setOnClickListener(new View.OnClickListener() { 134 | @Override 135 | public void onClick(View v) { 136 | downloadManager.cancelDownload(url2, true); 137 | } 138 | }); 139 | } 140 | 141 | 142 | static DownloadListener listener1 = new DownloadListener() { 143 | @Override 144 | public void onStartDownload(TextView textView) { 145 | Log.d(TAG, "1 onStartDownload() called"); 146 | } 147 | 148 | @Override 149 | public void onProgress(long downloaded, long total, TextView textView) { 150 | Log.d(TAG, "1 onProgress() called with: downloaded = [" + downloaded + "], total = [" + total + "]"); 151 | textView.setText(getPercentString(downloaded, total)); 152 | } 153 | 154 | @Override 155 | public void onPauseDownload(TextView textView) { 156 | Log.d(TAG, "1 onPauseDownload() called"); 157 | textView.setText("暂停"); 158 | } 159 | 160 | @Override 161 | public void onCancelDownload(TextView textView) { 162 | Log.d(TAG, "1 onCancelDownload() called"); 163 | textView.setText("已取消"); 164 | } 165 | 166 | @Override 167 | public void onFinishDownload(String file, TextView textView) { 168 | Log.d(TAG, "1 onFinishDownload() called:" + file); 169 | textView.setText("已完成"); 170 | } 171 | 172 | @Override 173 | public void onFail(String errorInfo, TextView textView) { 174 | Log.d(TAG, "1 onFail() called with: errorInfo = [" + errorInfo + "]"); 175 | textView.setText("下载出错"); 176 | } 177 | }; 178 | 179 | 180 | static DownloadListener listener2 = new DownloadListener() { 181 | @Override 182 | public void onStartDownload(TextView textView) { 183 | Log.d(TAG, "2 onStartDownload() called"); 184 | } 185 | 186 | @Override 187 | public void onProgress(long downloaded, long total, TextView textView) { 188 | Log.d(TAG, "2 onProgress() called with: downloaded = [" + downloaded + "], total = [" + total + "]"); 189 | tvProgress2.setText(getPercentString(downloaded, total)); 190 | } 191 | 192 | @Override 193 | public void onPauseDownload(TextView textView) { 194 | Log.d(TAG, "2 onPauseDownload() called"); 195 | } 196 | 197 | @Override 198 | public void onCancelDownload(TextView textView) { 199 | Log.d(TAG, "2 onCancelDownload() called"); 200 | } 201 | 202 | @Override 203 | public void onFinishDownload(String file, TextView textView) { 204 | Log.d(TAG, "2 onFinishDownload() called:" + file); 205 | } 206 | 207 | @Override 208 | public void onFail(String errorInfo, TextView textView) { 209 | Log.d(TAG, "2 onFail() called with: errorInfo = [" + errorInfo + "]"); 210 | } 211 | }; 212 | 213 | private static String getPercentString(long val1, long val2) { 214 | if (val2 > 0) { 215 | int val = (int)(val1*100/val2); 216 | return val+"%"; 217 | } else { 218 | if (val1 < 1024) { 219 | return val1+"bytes"; 220 | } else if (val1 < 1024 * 1024) { 221 | return (val1/1024)+"KB"; 222 | } else if (val1 < 1024 * 1024 * 1024) { 223 | return (val1/1024/1024)+"MB"; 224 | } else { 225 | return (val1/1024/1024/1024)+"GB"; 226 | } 227 | } 228 | } 229 | 230 | @Override 231 | protected void onStop() { 232 | super.onStop(); 233 | // downloadManager.pauseDownload(url1); 234 | // downloadManager.pauseDownload(url2); 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_first.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |