android:跳转支付宝转账工具类


  ps:本人小白,希望通过博客记录学习的点滴并与大家分享,欢迎大家留言交流!
 工具类MyALiPayUtil实现:Android 不集成支付宝sdk实现收款功能 通过Intent调起支付宝支付
 
 使用方式:1、调用hasInstalledAlipayClient方法判断,2、调用startAlipayClient方法跳转
 
 参考:http://www.jb51.net/article/131370.htm  作者:听着music睡
 参考:https://blog.csdn.net/u010399316/article/details/54892081 作者:空心菜的爱
 如有侵权或是原文作者感到不适的地方请联系本人删帖
使用示例:
case R.id.btn_to_shop:
    if (MyALiPayUtil.hasInstalledAlipayClient(this)){
        MyALiPayUtil.startAlipayClient(this,ALIPAY_SHOP);
    }
    else {
        Toast.makeText(this, "未检测到支付宝!", Toast.LENGTH_SHORT).show();
    }
    break;
//参数1:当前activity.this(this)
//参数2:支付宝二维码通过扫码工具解析得到的字符串。二维码解析工具如:草料二维码扫描器
//示例:个人二维码解析所得
public static final String ALIPAY_SHOP = "HTTPS://QR.ALIPAY.COM/FKX01612QIQBWHJTINTGC9";

MyALiPayUtil工具类如下:
public class MyALiPayUtil {

    // 支付宝包名
    private static final String ALIPAY_PACKAGE_NAME = "com.eg.android.AlipayGphone";

    //第一步:检查支付宝是否安装
    public static boolean hasInstalledAlipayClient(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(ALIPAY_PACKAGE_NAME, 0);
            return info != null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    //第二步:调用者调用此方法跳转到支付宝
    public static boolean startAlipayClient(Activity activity, String urlCode) {
        return startIntentUrl(activity, doFormUri(urlCode));
    }

  //格式化urlCode
    private static String doFormUri(String urlCode) {
        try {
            urlCode = URLEncoder.encode(urlCode, "utf-8");
        } catch (Exception e) {
        }
        final String alipayqr = "alipayqr://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=" + urlCode;
        String openUri = alipayqr + "%3F_s%3Dweb-other&_t=" + System.currentTimeMillis();
        return openUri;
    }

    //主要功能代码:跳转到支付宝
    private static boolean startIntentUrl(Activity activity, String intentFullUrl) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentFullUrl));
            activity.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
}

你可能感兴趣的:(android)