本文章将对APP内部直接跳转到某账号的支付宝转账界面进行详细阐述。这种方法适用于个人开发,仅完成了跳转支付,没有支付回调的信息。本文在跳转至支付宝时会弹出对话框以确认用户是否误操作。
1.你需要先获取转账对象收钱码的URLcode。
方法如下:
进入草料二维码扫描器草料二维码扫描器
可以选择三种方式对收钱码进行扫描,扫描后会出现二维码结果,红框框住的即为二维码的URLcode(这里在支付宝中同一个账号的收款码有可能不同,但是只需要最后一个‘/’的后面全部内容即可)。
2.支付界面的布局(activity_pay.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<Button
android:id="@+id/btn_pay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="支付" />
</LinearLayout>
3.支付界面(PayActivity.java)
public class PayActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_pay:
DialogUtils.showDonateDialog(this);
break;
}
}
}
4.两个工具类
(1)RomUtils,这个工具类用来检测手机相关信息
public class RomUtils {
private static final String TAG = "RomUtils";
//检测手机上是否安装某应用
public static boolean checkApkExist(Context context, String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
//获取本地软件版本号名称
public static String getLocalVersionName(Context ctx) {
String localVersion = "";
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
private static String getSystemProperty(String propName) {
String line;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
Log.e(TAG, "Unable to read sysprop " + propName, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
Log.e(TAG, "Exception while closing InputStream", e);
}
}
}
return line;
}
/**
* 判断是否为华为UI
*/
public static boolean isHuaweiRom() {
String manufacturer = Build.MANUFACTURER;
return !TextUtils.isEmpty(manufacturer) && manufacturer.contains("HUAWEI");
}
/**
* 判断是否为小米UI
*/
public static boolean isMiuiRom() {
return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"));
}
public static boolean isFlymeRom() {
return "flyme".equalsIgnoreCase(getSystemProperty("ro.build.user"));
}
}
(2)DialogUtils,这个工具类用来显示对话框
public class DialogUtils {
public static void showDonateDialog(Activity activity) {
final MaterialDialog dialog = new MaterialDialog(activity);
dialog.content("确认跳转到支付宝?").btnText("关闭", "确认").show();
//left btn click listener
//right btn click listener
dialog.setOnBtnClickL(
dialog::dismiss,
() -> {
if (RomUtils.checkApkExist(activity, "com.eg.android.AlipayGphone")) {
donate(activity);
} else {
Toasty.warning(activity, "本机未安装支付宝", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
);
}
//跳转到支付宝付款界面
private static void donate(Context context) {
String intentFullUrl = "intent://platformapi/startapp?saId=10000007&" +
"clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2F{URLcode}%3F_s" + //这里把{URLcode}替换成第一步扫描的结果
"%3Dweb-other&_t=1472443966571#Intent;" +
"scheme=alipayqr;package=com.eg.android.AlipayGphone;end";
try {
Intent intent = Intent.parseUri(intentFullUrl, Intent.URI_INTENT_SCHEME);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
对话框需要导入依赖包,详情请到github下载源码。
到这里就可以完成APP跳转到支付宝转账界面的功能。更完整的功能可以使用支付宝集成SDK,这个后续博客会进行详细的说明。