Android中订阅确认后如何跳转至应用落地页?
Android订阅确认后跳转应用落地页实现方案
核心逻辑
订阅支付完成后,必须在订阅确认成功的回调中触发落地页跳转,避免未确认就跳转导致的状态不一致问题。
完整代码实现
补全你提供的订阅确认代码,重点实现AcknowledgePurchaseResponseListener的回调逻辑,在确认成功后启动落地页:
// 初始化订阅确认监听 AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() { @Override public void onAcknowledgePurchaseResponse(BillingResult billingResult) { // 切换至主线程执行UI更新与页面跳转 runOnUiThread(() -> { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { // 订阅确认成功,更新本地状态 binding.tvSubsts.setText("Subscribed"); isSuccess = true; // 跳转至落地页(示例为ListView布局的Activity) Intent intent = new Intent(getApplicationContext(), SubscriptionLandingActivity.class); startActivity(intent); } else { // 订阅确认失败,处理错误逻辑 binding.tvSubsts.setText("Subscription Failed"); isSuccess = false; } }); } }; // 执行订阅确认操作 if (!purchase.isAcknowledged()) { AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener); }
落地页示例(ListView布局)
创建SubscriptionLandingActivity,布局文件示例:
<!-- res/layout/activity_subscription_landing.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="订阅专属内容" android:textSize="20sp" android:padding="16dp"/> <ListView android:id="@+id/content_list" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
落地页示例(BottomNavigationView布局)
如果需要底部导航样式的落地页,布局示例:
<!-- res/layout/activity_subscription_landing_bottom.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/content_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/bottom_nav_menu"/> </LinearLayout>
关键注意事项
- 跳转操作必须在主线程执行:BillingClient的回调运行在后台线程,需通过
runOnUiThread或Handler切换线程启动Activity。 - 落地页需在
AndroidManifest.xml中注册,避免启动失败。
内容的提问来源于stack exchange,提问作者Jezer Bayer Arces
相关产品推荐
相关产品推荐

