Andorid跳转支付宝转账

1.首先获取自己的收款二维码
2.拿到二维码去解析 推荐(https://cli.im/deqr)
3.准备工作完毕上代码(把二维码解析的数据修改到 PAYEE)
  // 支付宝包名
    private static final String ALIPAY_PACKAGE_NAME = "com.eg.android.AlipayGphone";
    private static final String PAYEE = "HTTPS://QR.ALIPAY.COM/fkx06205grpfwtsesdr2j20";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (hasInstalledAlipayClient(MainActivity.this)) {
                    startAlipayClient(MainActivity.this, PAYEE);
                } else {
                    Toast.makeText(MainActivity.this, "未检测到支付宝!请安装支付宝app在进行尝试~!", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }


    //检查支付宝是否安装
    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));
    }

    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;
        }
    }
4.效果图~
Andorid跳转支付宝转账_第1张图片
Android跳转支付宝页面演示.gif

你可能感兴趣的:(Andorid跳转支付宝转账)