WebView无法调起微信、支付宝 (net::ERR_UNKNOWN_URL_SCHEME)

WebViewClient webViewClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView wv, 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;
        }

        //处理http和https开头的url
        wv.loadUrl(url);
        return true;
    }
};
webview.setWebViewClient(webViewClient);

支付宝官方给的方法:

public boolean shouldOverrideUrlLoading(final WebView view, String url) { // 获取上下文, H5PayDemoActivity为当前页面 final Activity context = H5PayDemoActivity.this; // ------ 对alipays:相关的scheme处理 ------- if(url.startsWith("alipays:") || url.startsWith("alipay")) { try { context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url))); } catch (Exception e) { new AlertDialog.Builder(context) .setMessage("未检测到支付宝客户端,请安装后重试。") .setPositiveButton("立即安装", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Uri alipayUrl = Uri.parse("https://d.alipay.com"); context.startActivity(new Intent("android.intent.action.VIEW", alipayUrl)); } }).setNegativeButton("取消", null).show(); } return true; } // ------- 处理结束 ------- if (!(url.startsWith("http") || url.startsWith("https"))) { return true; } view.loadUrl(url); return true...
Show more
Show more in html format


版权声明:本文为xcdm100原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。