DownloadManager介绍
简介
DownloadManager是一个系统下载服务,提供了一套下载API,并可以通知栏显示下载进度。
API使用
获取DownloadManager对象
DownloadManager是一个系统服务,通过以下方式获取:
private DownloadManager mDownloadManager;
mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
初始化DownloadManager.Request
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(info.apkUrl));
String subPath = info.appName + "_" + info.apkVersion + ".apk";
request.setDestinationInExternalPublicDir("download", subPath);
request.setTitle(info.appName);
request.setMimeType("application/vnd.android.package-archive");
request.allowScanningByMediaScanner();
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
发送Request
将Request对象加入队列,系统就会自动启动下载,并返回一个唯一的id
mDownloadId = mDownloadManager.enqueue(request);
DownloadManager.Query查询下载进度
下载进度需要主动去查询,API并没有提供回调的接口:
private int[] getBytesAndStatus(long downloadId) {
int[] bytesAndStatus = new int[]{-1, -1, 0};
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = null;
try {
c = mDownloadManager.query(query);
if (c != null && c.moveToFirst()) {
bytesAndStatus[0] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytesAndStatus[1] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
bytesAndStatus[2] = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
}
} finally {
if (c != null) {
c.close();
}
}
return bytesAndStatus;
}
取消下载
传入之前返回的id即可取消本次下载操作
mDownloadManager.remove(mDownloadId);
广播监听下载完成
AndroidManifest.xml中声明接收器:
<receiver android:name="com.android.launcher3.promoted.AppDownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
</intent-filter>
</receiver>
public class AppDownloadBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = downloadManager.query(query);
boolean downloadSuccess = false;
String localPath = null;
String encodePath = null;
if (c != null && c.moveToFirst()) {
downloadSuccess = (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == downloadManager.STATUS_SUCCESSFUL);
int fileUriId = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String fileUri = c.getString(fileUriId);
LogUtils.d(TAG, "fileUri = "+fileUri);
if (Utilities.ATLEAST_N) {
if (fileUri != null) {
localPath = Uri.parse(fileUri).getPath();
encodePath = Uri.parse(fileUri).getEncodedPath();
}
}else {
localPath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
}
}
} else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
}
}
}
小结
DownloadManager作为系统组件,对于单个文件的下载很方便,但是对于需求复杂,或者批量并发下载的场景,不是很适合,有一定局限性,希望Google后面会给予更多的完善。