使用 Android 订阅限制功能

Limit Functionality with Android Subscriptions

我正在尝试为 android 应用程序创建应用内结算,以允许用户购买订阅。我已经设法做到了这一点并且我能够购买订阅,但我没有得到的是我如何将应用程序中的某些功能限制为那些没有订阅的人?

我似乎找不到任何相关教程。我想要的是在用户未订阅时按下按钮提示应用内结算 window。我可以用这段代码实现。

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);

}

但是如果用户已经订阅了怎么办?所以问题是,我如何检查用户是否订阅并执行按钮按下,如果没有显示账单 window。我能否仅在用户连接到 Internet 时获取该信息?我需要将该信息存储在设备上吗?

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

  1. 检查订阅是否已购买(responseCode == 7)
  2. 使用purchasesResult.getPurchasesList()
  3. 获取所有购买列表
  4. 在使用 onPurchasesUpdated 方法成功购买订阅后立即获得响应
  5. 使用 BillingFlowParams.Builder

    显示 In-App 计费流程
        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,您将值保存为已订阅),则显示高级功能,否则不显示。

希望对您有所帮助!