最近在做一个较简单的项目:通过一个webview来显示一个网页的App
这个网页有下载的功能,关于这一功能需要用到两个知识点:
1、webview监听网页的下载链接。(webview默认情况下是没有开启,在这个情况下,你会发现,在浏览器上可以正常下载的网页里的某个点击。在你的webview里面,点击是没有反应的);
2、使用系统的DownloadManager进行下载。
下载任务会显示在顶部
private void startDownload(String url) {dm = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));request.setMimeType("application/cn.trinea.download.file");request.setVisibleInDownloadsUi(true);request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"fileName"); request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);enqueue = dm.enqueue(request);
???????}
此时就能够在下拉,看到所下载的项目,点击,对下载的app进行安装。
3、注册监听
private DownloadListener mDownloadListener = new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {// TODO:处理下载事件startDownload(url)}};webView.setDownloadListener(mDownloadListener);
至此实现了:
1、webview监听网页的下载链接。
2、使用系统的DownloadManager进行下载。