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的组件化开发