直接干就是了,重写WebViewClient类,然后shouldOverrideUrlLoading中拦截url进行处理(一般调起支付宝会有多次回调,会接收到多个url,最后通过拦截识别订单url就可以跳转支付页面; 或者利用apipay_sdk提供的调起本地支付宝支付的方式):
/**
* 自定义MyWebClient
*/
private class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
///< 在开始加载网页时会回调
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
// ///< 交给Webview自己处理 - 也能解决加载百度后无法返回退出(不停的刷新百度)
// //view.loadUrl(url);
// //return true;
// return false;
///< 拦截支付宝支付
if (url.contains("platformapi/startapp")) {
Intent intent;
try {
intent = Intent.parseUri(url,
Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
startActivity(intent);
} catch (Exception e) {
//e.printStackTrace();
}
}
///< android 6.0 两种方式获取intent都可以跳转支付宝成功,7.1测试不成功
else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
&& (url.contains("platformapi") && url.contains("startapp"))) {
Intent intent;
try {
intent = Intent.parseUri(url,
Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
startActivity(intent);
} catch (Exception e) {
//e.printStackTrace();
}
}
///< 如果是下载的情况,则跳转到浏览器
else if (url.contains("download")) { ///< && url.contains(".apk")
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else {
view.loadUrl(url);
}
return true;
///< 调用Native支付 - 接alipaySdk-20180601.jar - https://docs.open.alipay.com/203/106493/
// if (!(url.startsWith("http") || url.startsWith("https"))) {
// return true;
// }
//
// /**
// * 推荐采用的新的二合一接口(payInterceptorWithUrl),只需调用一次
// */
// final PayTask task = new PayTask(ExternalLinkerDetailActivity.this);
// boolean isIntercepted = task.payInterceptorWithUrl(url, true, new H5PayCallback() {
// @Override
// public void onPayResult(final H5PayResultModel result) {
// ///< 支付结果返回
// final String url = result.getReturnUrl();
// if (!TextUtils.isEmpty(url)) {
// ExternalLinkerDetailActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// view.loadUrl(url);
// }
// });
// }
// }
// });
//
// /**
// * 判断是否成功拦截
// * 若成功拦截,则无需继续加载该URL;否则继续加载
// */
// if (!isIntercepted) {
// view.loadUrl(url);
// }
// return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!bIsError) {
///< 页面加载结束...
showStateView();
}
bIsError = false;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
///< 加载错误的时候会回调,在其中可做错误处理,比如再请求加载一次,或者提示404的错误页面
super.onReceivedError(view, errorCode, description, failingUrl);
bIsError = true;
retryStateView();
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
///< 在每一次请求资源时,都会通过这个函数来回调
return super.shouldInterceptRequest(view, request);
}
重点看下shouldOverrideUrlLoading里面的关于支付宝的两种方式就行了。其他方法不用管。
2. 关于alipay_sdk这里提供一个文档供大家对接Native支付 开放平台文档中心
引入下就可以使用了
image
调用本地有个问题就是中间会产生一个空白页面,应该是处理回调事件的。由于没用这种方式,所以就没有专门去研究。如果有用过的记得分享下哟!谢谢了哈.....
随着产品需求,可能会遇到更多的场景,尽量看下支付官方是否提供,然后不行再查下相关资料以及网友的分享,这样应该会比较快点。不过基本的解决思路我们还是有必要了解-比如根据微信支付宝等订单呀,参数呀等看是否能通过自主分析解决一些个问题,有些场景可能别人也没用过,只能自己琢磨了! 当然可能需要自己提升更多的知识技能才行...汗颜呀.....