WebView爬坑之旅4--网页内下载
来源:互联网 发布:淘宝哪里输入淘口令 编辑:程序博客网 时间:2024/05/22 13:06
webView中下载,这个功能在我们的产品中还挺重要的,webView中默认的实现方式好像是跳转第三方应用打开,比如手机内置浏览器或者其他有下载能力的应用,我的实现呢,也是从最原始的方法到现在自己控制在webview中下载,效果的话当然是自己实现下载更好,talk is cheap,look the code:
/** * Created by ly on 2017/9/9 10:15. */public class CustomDownloadListener implements DownloadListener { private AppCompatActivity activity; private static final int PARSE_COMPLETE = 1; private static final String FILE_NAME = "fileName"; private static final String DOWNLOAD_URL = "url"; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == PARSE_COMPLETE) { final Bundle bundle = (Bundle) msg.obj; try { new AlertDialog.Builder(activity) .setTitle(activity.getString(R.string.dialog_title_hint)) .setMessage("确定下载" + bundle.getString(FILE_NAME) + "?") .setPositiveButton("下载", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DownLoadService.addLoadTask(activity, bundle.getString(DOWNLOAD_URL)); } }) .setNegativeButton(activity.getString(R.string.update_dialog_cancel1), null) .setCancelable(false) .show(); } catch (Exception e) { e.printStackTrace(); } } } }; public CustomDownloadListener(AppCompatActivity activity) { this.activity = activity; } @Override public void onDownloadStart(final String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { if (TextUtils.isEmpty(url) || activity == null) { Logger.w("onDownloadStart url/activity is null return..."); return; } parseFileName(url);// Uri uri = Uri.parse(url);// Intent intent = new Intent(Intent.ACTION_VIEW, uri);// context.startActivity(intent); } /** * 根据下载链接解析源文件名 * Created by ly on 2017/9/20 11:46 */ private void parseFileName(final String url) { new Thread(new Runnable() { @Override public void run() { String filename = null;//要下载的文件的文件名 try { URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); try { // 转换编码 String contentDisposition = URLDecoder .decode(conn.getHeaderField("content-Disposition"), "UTF-8"); filename = contentDisposition.substring(contentDisposition .indexOf("filename=") + 9, contentDisposition.length()); if (!TextUtils.isEmpty(filename)) { if (filename.contains("\"")) { filename = filename .substring(filename.indexOf("\""), filename.lastIndexOf("\"")); if (filename.contains("\"")) filename = filename.replace("\"", ""); } } } catch (Exception e) { e.printStackTrace(); } if (TextUtils.isEmpty(filename)) { filename = u.getFile().substring( u.getFile().lastIndexOf('/') + 1, u.getFile().length()); } } catch (Exception e) { e.printStackTrace(); filename = url.substring(url.lastIndexOf('/') + 1, url.length()); } Bundle bundle = new Bundle(); bundle.putString(FILE_NAME, filename); bundle.putString(DOWNLOAD_URL, url); handler.obtainMessage(PARSE_COMPLETE, bundle).sendToTarget(); } }).start(); }
fk,这个代码的样式真是没谁了(原谅我写代码能一行搞定就不写两行的习惯),太长就显示不下,将就着看吧,其中具体的下载文件的代码我就不贴了
到现在webview相关的坑点已经写完,以后不知道会不会遇到新的坑,如果有就继续追加更新,没有就,啊哈哈哈哈
对了,其实我想把相关代码封装成一个自定义webview放GitHub的(发现一个别人开源的,看起来还可以AdvancedWebView),但想想,就这样吧,也就这么点东西,自己记录记录好了,有什么不对的地方,还请各位大佬指点指点
等下次有时间,再分享一下android的组件化开发
阅读全文
0 0
- WebView爬坑之旅4--网页内下载
- 加载网页之webview
- Android爬坑之旅之WebView
- Android爬坑之旅之WebView
- Android之WebView加载网页
- intent://platformapi/startapp?--Webview内跳转到支付宝网页
- iOS使用WebView简单地应用内显示网页
- 安卓webview实现应用内嵌入网页
- iOS使用WebView简单地应用内显示网页
- iOS使用WebView简单地应用内显示网页
- Android使用WebView嵌入网页,网页内点击跳转到另一个网页后,返回问题解决
- Android学习之利用WebView打开网页
- Android之WebView网页滚动截图
- Android之 WebView(网页控件)使用方法
- 安卓网络轻轻松松编程2-------下载网页,初始webView
- Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity
- Android WebView详解之文件下载
- 通过python进行单网页内图片爬取及下载
- Python读写txt文本文件的操作方法全解析
- 11月9日云栖精选夜读:阿里90后工程师,如何用AI程序写出双11打call歌?
- 11月16日比特币硬分叉取消,保皇派core和革命派矿主战端难解难分
- springboot接收json入参
- robot framework之chrome浏览器与firefox浏览器使用区别
- WebView爬坑之旅4--网页内下载
- xiaogui9527 使用X.509数字证书加密解密实务(三)-- 使用RSA证书结合对称加密技术加密长数据
- 【转】【Quartz】Quartz概述及入门实例
- libevent evbuffer
- Webpack 条件匹配resouce和issuer分别是对被请求文件和模块文件的匹配
- System.TypeInitializationException
- Java中使用Jedis操作Redis
- js 获取前天、昨天、今天、明天、后天的时间
- 学习的开始,学习Python的输入与输出