
全栈工程师开发手册 (作者:栾鹏)
安卓Download Manager下载管理器全解:启动下载,监听点击下载通知,设置下载完成函数。
使用Download Manager启动下载文件,返回下载线程id
private long download(Context context,String path) {
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager downloadManager;
downloadManager = (DownloadManager)(context.getSystemService(serviceString)); //获取系统服务
Uri uri = Uri.parse(path);
DownloadManager.Request request = new Request(uri); //下载请求对象,可以addRequestHeader添加HTTP报头,setMimeType重写服务器返回的MIME类型
request.setAllowedNetworkTypes(Request.NETWORK_WIFI); //指定下载条件为wifi
request.setTitle("通知栏标题"); //设置通知栏Notification的样式
request.setDescription("通知栏描述");//设置通知栏Notification的样式
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE); //设置通知栏的显示。VISIBILITY_VISIBLE持续显示,完成后移除,
//VISIBILITY_VISIBLE_NOTIFY_COMPLETED持续显示,完成后保持,
//VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION完成后显示,
//VISIBILITY_HIDDEN隐藏
//request.setDestinationUri(Uri.fromFile(file)); //指定文件存储到任意位置
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, "aa.png"); //指定文件存储到外部文件夹中
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DCIM, "aa.png"); //指定文件存储到公共目录
request.allowScanningByMediaScanner(); //设置下载的文件可以被媒体扫描器扫描
request.setVisibleInDownloadsUi(true); //设置下载的文件对系统的download应用是可见可管理的
long id = downloadManager.enqueue(request); //enqueue方法,一旦连接可用,就会开始下载,返回下载进程id
//downloadManager.remove(id); //取消下载,终止下载 删除下载,返回成功取消的数目
return id;
}
设置指定下载完成后执行某函数,传入下载线程id
private void complete(Context context,final long id) {
final DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
//监视下载进度
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO 自动生成的方法存根
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id == reference) {
//对下载的文件进行一些操作
Query myDownloadQuery = new Query(); //查询器查询下载请求的状态,进度和详细信息
myDownloadQuery.setFilterById(reference); //传入下载进程的id
//myDownloadQuery.setFilterByStatus(DownloadManager.STATUS_PAUSED); //使用setFilterByStatue过滤下载状态,被暂停的下载
Cursor myDownload = downloadManager.query(myDownloadQuery); //获取查询的数据结果,Cursor是一种数据结构
//查询结果中的第一个。可以while(myDownload.moveToNext())遍历结果
if (myDownload.moveToFirst()) {
int fileNameIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME); //文件名称,
int fileUriIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); //下载地址
// int reasonIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_REASON); //暂停原因
// int titleIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_TITLE); //下载标题
// int fileSizeIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES); //文件总大小
// int bytesDLIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR); //剩余下载大小
String fileName = myDownload.getString(fileNameIdx);
String fileUri = myDownload.getString(fileUriIdx);
// String title = myDownload.getString(titleIdx);
// int fileSize = myDownload.getInt(fileSizeIdx);
// int bytesDL = myDownload.getInt(bytesDLIdx);
// int reason = myDownload.getInt(reasonIdx); // 将暂停原因转化为友好的文本
// String reasonString = "Unknown";
// switch (reason) {
// case DownloadManager.PAUSED_QUEUED_FOR_WIFI :
// reasonString = "Waiting for WiFi"; break;
// case DownloadManager.PAUSED_WAITING_FOR_NETWORK :
// reasonString = "Waiting for connectivity"; break;
// case DownloadManager.PAUSED_WAITING_TO_RETRY :
// reasonString = "Waiting to retry"; break;
// default : break;
// }
//对文件进行一些操作
Log.d("网络操作", fileName + " : " + fileUri);
}
myDownload.close(); //关闭结果
}
}
};
((Activity)context).registerReceiver(receiver, filter);
}
设置单击下载通知的事件响应,传入下载线程id
private void Download_Notification(Context context,final long id) {
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String extraID = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
long[] references = intent.getLongArrayExtra(extraID);
for (long reference : references)
if (reference == id) {
// 对下载文件进行处理
}
}
};
((Activity)context).registerReceiver(receiver, filter);
}