前言
APP中难免会使用到下载文件、更新APP的功能,而我们自己处理下载流程往往都很复杂的,要考虑http请求、下载进度监听、下载UI提示信息等等。google官方已经封装好了专门用于下载的类,我们可以直接使用这个类进行下载即可。
DownloadManger是android 2.3(API 9)开始提供的系统服务,用于处理长时间的下载操作。应用场景是客户端请求一个URL地址去下载一个目标文件。DownloadManger可以构建一个后台下载服务,在发生故障或连接更改、重新启动系统等情况后,处理HTTP连接并重试下载。
代码
下载监听接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hyfun.downloadmanager.support;
public interface AndroidDownloadManagerListener { void onPrepare();
void onSuccess(String path);
void onFailed(Throwable throwable); }
|
下载管理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| package com.hyfun.downloadmanager.support;
import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.util.Log;
import java.io.File;
public class AndroidDownloadManager { private DownloadManager downloadManager; private Context context; private long downloadId; private String url; private String name;
private String path;
private AndroidDownloadManagerListener listener;
public AndroidDownloadManager(Context context, String url) { this(context, url, getFileNameByUrl(url)); }
public AndroidDownloadManager(Context context, String url, String name) { this.context = context; this.url = url; this.name = name; }
public AndroidDownloadManager setListener(AndroidDownloadManagerListener listener) { this.listener = listener; return this; }
public void download() { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setAllowedOverRoaming(false); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); request.setTitle(name); request.setDescription("文件正在下载中......"); request.setVisibleInDownloadsUi(true);
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), name); request.setDestinationUri(Uri.fromFile(file)); path = file.getAbsolutePath();
if (downloadManager == null) { downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); } if (downloadManager != null) { if (listener != null) { listener.onPrepare(); } downloadId = downloadManager.enqueue(request); }
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); }
private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); switch (status) { case DownloadManager.STATUS_PAUSED: break; case DownloadManager.STATUS_PENDING: break; case DownloadManager.STATUS_RUNNING: break; case DownloadManager.STATUS_SUCCESSFUL: if (listener != null) { listener.onSuccess(path); } cursor.close(); context.unregisterReceiver(receiver); break; case DownloadManager.STATUS_FAILED: if (listener != null) { listener.onFailed(new Exception("下载失败")); } cursor.close(); context.unregisterReceiver(receiver); break; } } } };
private static final String getFileNameByUrl(String url) { String filename = url.substring(url.lastIndexOf("/") + 1); filename = filename.substring(0, filename.indexOf("?") == -1 ? filename.length() : filename.indexOf("?")); return filename; }
}
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| new AndroidDownloadManager(this, url) .setListener(new AndroidDownloadManagerListener() { @Override public void onPrepare() { Log.d(TAG, "onPrepare"); }
@Override public void onSuccess(String path) { Log.d(TAG, "onSuccess >>>>" + path); }
@Override public void onFailed(Throwable throwable) { Log.e(TAG, "onFailed", throwable); } }) .download();
|