前段时间有个项目需要在H5页面里下载文档文件,由于项目需求这里采用Android系统自带DownloadManager来进行后台自动下载管理,我们只需要稍微设置几个参数就进行文件下载了,并且下载进行时和下载完成后会有通知显示,话不多说直接上代码:
由于是WebView中的操作,这里需要监听WebView的下载监听,每当WebView中有下载操作都会回调这个方法,代码如下:
web.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType, long contentLength) {
downloadySystem(url, contentDisposition, mimeType);
}
});
上面代码中只有要下载操作都会回调这个方法,我们只需要添加一个下载方法即可实现,以下是方法代码:
private void downloadySystem(String url, String contentDisposition, String mimeType) {
//指定下载地址
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//分许媒体扫描,根据下到的文件类剂被加入相册、音乐等姐体库
request.allowScanningByMediaScanner();
//设置通知的显示类型,下载进行时和完成后显示通知
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//设置通知栏的标题,如里不语警,默认伸用文件么
request.setDescription("下载成功");
//介许在计费流量下下载
request.setAllowedOverMetered(false);
//允许该记录在下载管理界面可见
request.setVisibleInDownloadsUi(true);
// 允许漫游时下载
request.setAllowedOverRoaming(true);
// 允许下载的网路类型WIFI 和 手机网络流量
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);// 设置下载文件保存的路径和文件名
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
String name = null;
try {
name = URLDecoder.decode(fileName, "utf-8");
}//这里需要转码获取到中文格式的文件名
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//设置存储路径
request.setDestinationInExternalFilesDir(this,"file_download/",name);
final DownloadManager downloadManager= (DownloadManager)this.getSystemService(DOWNLOAD_SERVICE);
// 下载任务唯一ID
long downloadId = downloadManager.enqueue(request);
}
- 如果各位不需要对下载成功后进行一些逻辑操作,这里就可以满足需要了,如果还需下载完成后自动打开文件那么请继续往下看
- 在前面下载中,下载完成后系统会自动发送一个广播来告知文件已下载完成,我们可以自定义一个广播来接收系统广播,由于代码较多,这里就不放图片了,直接上代码了如:
-
public class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { TipsUtil.log("监听到下载完成"); if (intent != null) { if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { ToastFactory.showToast(context, "下载完毕,即将打开下载文件"); //这里是下载文件的唯一ID,与下载时的ID相同 long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE); String type = downloadManager.getMimeTypeForDownloadedFile(downloadId); if (TextUtils.isEmpty(type)) { type = "*/*"; } //取到下载文件Uri Uri uri = downloadManager.getUriForDownloadedFile(downloadId); File file = new File(uri.getPath()); TipsUtil.log("文件Uri=" + uri.getPath() + "\n文件名称=" + file.getName()); if (uri != null) { Intent handlerIntent = new Intent(Intent.ACTION_VIEW); handlerIntent.setDataAndType(uri, type); //需要设置Flag 要不然会报错 handlerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); context.startActivity(handlerIntent); } } } } }
然后我们在Activity 或者Fragment里面进行注册广播如,由于本人是在Fragment中操作的所以要getActivity()方法,如果是在Activity中操作则不需要直接 registerReceiver(mReceiver,filter) 即可.