android通过webview调起支付宝app支付

android通过webview调起支付宝app支付
webview在加载网页的时候会默认调起手机自带的浏览器加载网页,用户体验不好。但当用户设置浏览器客户端(setWebViewClient)设置这样的监听事件之后,当请求url的时候就不会打开手机自带的浏览器。

webview.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            LoadingDialog.closeDialog();
        }

    });

在此方法中可以监听浏览器开始加载网页和加载网页结束。

今天着重讲的并不是上边的两个方法而是下面的这位

putinmoney_webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {}
});

shouldOverrideUrlLoading并不是每次都在onPageStarted之前开始调用的,只有在调用webview.loadURL的时候才会调用。

webview.loadUrl(“https://qr.alipay.com/bax05351pgjhc4yegd2y2084“);
但发起请求的时候后,webview的连续动作是先后请求两个url

1.https://mobilecodec.alipay.com/client_download.htm?qrcode=bax05351pgjhc4yegd2y2084

2.https://ds.alipay.com/from=mobilecodec&scheme=alipayqr%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%253A%252F%252Fqr.alipay.com%252Fbax05351pgjhc4yegd2y2084%253F_s%253Dweb-other

之后返回一个意图,也是用这个意图来打开支付宝app

intent://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2Fbax05351pgjhc4yegd2y2084%3F_s%3Dwebother&_t=1474448799004#Intent;scheme=alipayqr;package=com.eg.android.AlipayGphone;end

webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.e(TAG, "访问的url地址:" + url);
            if (parseScheme(url)) {
                try {
                    Uri uri = Uri.parse(url);
                    Intent intent;
                    intent = Intent.parseUri(url,
                            Intent.URI_INTENT_SCHEME);
                    intent.addCategory("android.intent.category.BROWSABLE");
                    intent.setComponent(null);
                    // intent.setSelector(null);
                    startActivity(intent);

                } catch (Exception e) {

                }
            } else {
                view.loadUrl(url);
            }

            return true;

        }

    });

此外要对webview设置下:

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
// 设置支持缩放
webSettings.setBuiltInZoomControls(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// webSettings.setDatabaseEnabled(true);

// 使用localStorage则必须打开
webSettings.setDomStorageEnabled(true);

webSettings.setGeolocationEnabled(true);

实例下载地址:https://github.com/reachchen/loadurltoali.git

在项目中发现的问题在此修正:

发现一些手机(例如一些升级了7.0的手机)在请求后并没有返回intent,所以就会出现调不起支付宝的情况。但是用浏览器直接打开是可以的

兼容的思路如下:

新建一个线程,在用户发出请求后,如果10秒的时间没有收到调起的intent,就用浏览器直接调起。

代码如下:

public boolean parseScheme(String url) {
    if (url.contains("platformapi/startapp")){
        return true;
    } else if(url.contains("web-other")){
        return false;
    }else {
        return false;
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值