[Android] webview调起支付宝支付页面记录

简书地址

项目需求:接收一个支付宝二维码支付url,通过webview打开对应的支付宝app支付页面;
之前在网上找了段代码结果适配有问题,然后就顺便研究下,记录(liu)在(shui)此(zhang);

测试过的机型 : 红米1s(4.4.4), 华为TAG-AL100(5.1), 华为KNT-AL20(6.0), exus6p(7.1.1);

接收到的支付链接 : url=https://qr.alipay.com/stx01744jxpniu1ijb5wr7d
P.S. 具体规则可以看 蚂蚁金服文档

之前查看到的文章大多类似 这篇;

主要代码:

//启动支付宝,并跳转到付款页面
if (url.contains("platformapi/startapp")) {
    startAlipayActivity(url);
}

private void startAlipayActivity(String url) {
    LogUtils.d("alipay", "startUp");
    Intent intent;
    try {
        intent = Intent.parseUri(url,
                Intent.URI_INTENT_SCHEME);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setComponent(null);
        startActivity(intent);
        LogUtils.d("alipay", "start intent = " + intent.toString());
    } catch (Exception e) {
        e.printStackTrace();
        LogUtils.d("alipay", "error " + e.getMessage());
    }
}

不过运气不好,我手上的机子Nexus6P系统是7.1.1的,运行不成功,一直停留在网页中,并未跳转,直接使用断点chrome://inspect功能,发现最终的url是被encode过的,难怪条件不匹配,后来尝试了UrlDecode在判断,依然不行,然后尝试单独判断两个关键字,也是调用不成功,郁闷,在网上没搜索到现成的方法,就按照 上面文章 的思路,自己跟踪一下url跳转执行情况看看:

测试代码

mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setDomStorageEnabled(true);

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // 判断方法1 ,注释掉方法2再测试
        if (url.contains("platformapi/startapp")) {
            startAlipayActivity(url);
        }else {
            mWebView.loadUrl(url)
        }
     return true;

     // 判断方法2,注释掉方法1再测试
     if (url.contains("platformapi") && url.contains("startapp")) {
            startAlipayActivity(url);
        } else {
            iew.loadUrl(url)
        }
    return true;
    }
    ......
}

分别测试方法1和方法2,看看两种调用的情况,四台机子测试结果:

  1. 原始 urlOri = https://qr.alipay.com/stx01744jxpniu1ijb5wr7d
  2. 第一次加载返回: urlFirst = https://mobilecodec.alipay.com/client_download.htm?qrcode=stx01744jxpniu1ijb5wr7d
  3. 第二次加载返回: urlHttps = https://ds.alipay.com/?from=mobilecodec&scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%253A%252F%252Fqr.alipay.com%252Fstx01744jxpniu1ijb5wr7d%253F_s%253Dweb-other
  4. 这个时候通过判断 url.contains("platformapi/startapp")) 自然会由于url编码的原因判断失败,再次调用 mWebView.loadUrl(urlHttps) :
  5. 此时:
    • 对于4.4/5.1/6.0机子的 WebView 来说:
      1. 继续加载 urlHttps 的话会返回一个intent : urlIntent = intent://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2Fstx01744jxpniu1ijb5wr7d%3F_s%3Dweb-other&_t=1483682394666#Intent;scheme=alipays;package=com.eg.android.AlipayGphone;end
      2. 通过解析它获取到 targetIntent = Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://ds.alipay.com/... }
      3. 成功调起支付宝App;
    • 而对于Nexus6p 7.1.1系统来说,返回的url还是那个 urlHttps, 无法满足条件,也自然无法进行解析;
  6. 而如果通过分开判断两个关键字 (url.contains("platformapi") && url.contains("startapp")) 来解析intent的话,4.4/5.1机子的webview无法正常跳转,而6.0/7.1.1的机子则正常;

适配方案

综上, 在6.0及以上机子,可以直接对支付宝服务器返回的https url进行解析并调起支付宝app跳转到指定的支付页面,而之前的机型则直接通过webview对https url进行加载,会返回一个inent url再进行跳转:

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("platformapi/startapp")) {
            startAlipayActivity(url);
            // android  6.0 两种方式获取intent都可以跳转支付宝成功,7.1测试不成功
        } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                && (url.contains("platformapi") && url.contains("startapp"))) {
            startAlipayActivity(url);
        } else {
            mWebView.loadUrl(url)
        }
        return true;
    }
}

// 调起支付宝并跳转到指定页面
private void startAlipayActivity(String url) {
    Intent intent;
    try {
        intent = Intent.parseUri(url,
                Intent.URI_INTENT_SCHEME);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setComponent(null);
        startActivity(intent);
        finish();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

扩展,网页调起原生app

从浏览器或者Webview中唤醒APP
如何在浏览器中点击链接启动App

官网这篇 Android Intents with Chrome 也解释很清楚了,在 AndroidManifest.xml 中需要指定目标Activity的 category 属性,添加 android.intent.category.BROWSABLE ,当然还要指定跳转url的 host/scheme等值才行,比如:

<activity
    android:name="YourActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>

        <data
            android:host="platformapi.startapp.redeemcode"
            android:scheme="uplusgo"/>
    intent-filter>
activity>

就可以在html页面中设置a标签指定其链接为 uplusgo://platformapi.startapp.redeemcode?your_parameter_name=value , 点击启动应用,在Activity中通过如下方式来获取附加参数:

Intent mIntent = getIntent();
Uri uri = mIntent.getData();
if (uri != null) {
     String value = uri.getQueryParameter("your_parameter_name");
}

你可能感兴趣的:(Android)