Android O webview兼容,Android WebView 踩坑之路

H5定位

// 定位(location)

settings.setGeolocationEnabled(true);

String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();

//设置定位的数据库路径

settings.setGeolocationDatabasePath(dir);

webView.setWebChromeClient(new WebChromeClient() {

@Override

public void onReceivedIcon(WebView view, Bitmap icon) {

super.onReceivedIcon(view, icon);

}

// 处理javascript中的alert

public boolean onJsAlert(WebView view, String url, String message,

final JsResult result) {

return true;

}

// 处理javascript中的confirm

public boolean onJsConfirm(WebView view, String url,

String message, final JsResult result) {

return true;

}

@Override

public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {

callback.invoke(origin, true, false);

super.onGeolocationPermissionsShowPrompt(origin, callback);

}

});

Http、Https混杂图片无法加载

WebSettings settings = webView.getSettings();

settings.setJavaScriptEnabled(true);

settings.setLoadWithOverviewMode(true);

settings.setBlockNetworkImage(false);

settings.setAppCacheEnabled(true); // 默认值 false

settings.setLoadsImagesAutomatically(true); // 是否自动加载图片

// 存储(storage)

settings.setDomStorageEnabled(true); // 默认值 false

settings.setDatabaseEnabled(true);

settings.setCacheMode(WebSettings.LOAD_DEFAULT);

@Override

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

//super.onReceivedSslError(view, handler, error);

// handler.proceed();// 接受所有网站证书

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

view.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}

}

微信、支付宝、打电话无法调起

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url == null) return false;

try {

if (url.startsWith("weixin://") || url.startsWith("alipays://") ||

url.startsWith("mailto://") || url.startsWith("tel://")

//其他自定义的scheme

) {

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(intent);

return true;

}

} catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)

return false;

}

view.loadUrl(url);

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值