【问题标题】:Limit Functionality with Android Subscriptions使用 Android 订阅限制功能
【发布时间】:2019-11-11 10:06:02
【问题描述】:

我正在尝试为允许用户购买订阅的 Android 应用程序创建应用内计费。我已经设法做到了,并且可以购买订阅,但我没有得到的是如何将应用程序中的某些功能限制为那些尚未订阅的人?

我似乎找不到任何关于此的教程。如果用户没有订阅提示应用内计费窗口,我想要的是按下按钮。我可以用这段代码来实现。

public void launchBillingFLow(@SkuType String skuType, String productId) {

    Runnable launchBillingRequest = () -> {

        BillingFlowParams mBillingFlowParams;

        mBillingFlowParams = BillingFlowParams.newBuilder()
                .setSku(productId)
                .setType(skuType)
                .build();

        mBillingClient.launchBillingFlow((Activity) context, mBillingFlowParams);

    };

    executeServiceRequest(launchBillingRequest);

}

但是如果用户已经订阅了呢?所以问题是,我如何检查用户是否订阅并执行按钮按下,如果不显示计费窗口。只有当用户连接到互联网时,我才能获得该信息吗?我需要在设备上存储这些信息吗?

【问题讨论】:

  • 我相信你应该使用 queryPurchases,它使用 Google Play 缓存而不发起网络请求。因此,您将能够知道谁购买了哪种产品,然后进行有限访问。

标签: android in-app-purchase billing in-app-subscription


【解决方案1】:

使用此订阅方式获取购买详情 -

  1. 检查是否已购买订阅(responseCode == 7)
  2. 使用 purchaseResult.getPurchasesList() 获取所有购买列表
  3. 在使用 onPurchasesUpdated 方法成功完成订阅后获取响应
  4. 使用 BillingFlowParams.Builder 显示应用内结算流程

        mBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
    
                    if (billingResponseCode == BillingClient.BillingResponse.OK) {
                        // The billing client is ready. You can query purchases here.
    
                        final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
                        for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
                            if (sourcePurchase != null) {
    
                                ConsumeResponseListener listener = new ConsumeResponseListener() {
                                    @Override
                                    public void onConsumeResponse(final int responseCode, final String purchaseToken) {
                                        // Log.d("anupam2", responseCode + "  <------->  "+ purchasesResult.getPurchasesList() + "  <------->  " + purchaseToken);
                                    }
                                };
                                mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
                            } else {
    
                            }
                        }
    
                        if (purchasesResult.getPurchasesList().size() > 0) {
                            //  Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");
    
                        } else {
                            //  Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
                            BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
                            int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
                            if (responseCode == 7) {
    
                                //Item already purchased
                            }
                        }
                    }
                }
    
                @Override
                public void onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
                }
            });
    
    mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
                @Override
                public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    
                    //  Log.d("anupam", responseCode + "");
                    if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
                        for (Purchase purchase : purchases) {
                        //List of purchases
                        }
                    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                        Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
                    }
                }
            }).build();
    

现在您可以在启动画面中调用它,并根据响应代码在应用中使用 sharedpreference 或全局变量保存一个值。检查您的订阅 ID 的响应代码是否为 7。如果已订阅(对于 responseCode == 7,您将值保存为已订阅),否则不显示高级功能。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2021-02-01
    相关资源
    最近更新 更多