Android原生webview打开支付宝

最近解决一个问题,后台返回客户单的是一个支付宝的链接,需要客户端这边来打开,返回地址为:

https://openapi.alipay.com/gateway.do?app_id=***

使用webview加载后会重定向到

https://mclient.alipay.com/cashier/mobilepay.htm?alipay_exterface_invoke_assign_target=

再然后会打开支付宝自定义的scheme

alipays://platformapi/startApp?appId=

这里就需要在shouldOverrideUrlLoading里面添加如下代码:

            if (url.startsWith("http")) {
                return super.shouldOverrideUrlLoading(view, url);
            } else {
                try {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    finish();
                    return true;
                } catch (Exception e) {//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    return true;//没有安装该app时,返回true,表示拦截自定义链接,但不跳转,避免弹出上面的错误页面
                }

            }

webview的setting设置如下

    private void setting(WebView dWebView) {
        dWebView.setWebViewClient(client);
        dWebView.setWebChromeClient(new WebChromeClient());
        WebSettings webSetting = dWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);//这句很重要
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setDisplayZoomControls(false);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        //下面两行代码可以防止webview页打开黑屏
        dWebView.setBackgroundColor(App.getInstance().getColor(android.R.color.transparent));
        dWebView.setBackgroundResource(R.color.white);


    }

你可能感兴趣的:(android)