IabHelper:检查订阅是否自动续订

时间:2017-01-04 15:28:10

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

我有一个带有订阅的Android应用,我使用IabHelper来查询用户是否有有效订阅。现在我还需要知道此订阅是否自动续订。

我正在查询订阅状态:

private boolean userIsSubscribed() {
    ArrayList<String> skuList = new ArrayList<>();
    skuList.add(PREMIUM_SUBSCRIPTION);
    // Check the subscription status
    try {
        Inventory inventory = iabHelper.queryInventory(true, null, skuList);
        Purchase purchase = inventory.getPurchase(PREMIUM_SUBSCRIPTION);
        // Is the user subscribed?
        if (purchase != null && purchase.getPurchaseState() == 0) {
            // User is subscribed
            Log.d(TAG, "Subscribed.");
            // TODO check if subscription is auto-renewing
            return true;
        } else {
            // User is not subscribed
            Log.d(TAG, "Not subscribed.");
            return false;
        }
    } catch (IabException | NullPointerException e) {
        // There was an error with the subscription process
        Log.e(TAG, e.getMessage(), e);
        return false;
    } catch (IllegalStateException e) {
        // IabHelper not set up
        Log.e(TAG, e.getMessage(), e);
        return false;
    }
}

遗憾的是,Purchase对象没有请求autoRenewal字段的方法。有没有其他方法可以获得这些信息?

1 个答案:

答案 0 :(得分:2)

调用Purchase#isAutoRenewing方法。如果缺少此方法,则可能需要更新引用的IAB文件:

    if (purchase != null && purchase.getPurchaseState() == 0) {
        // User is subscribed
        Log.d(TAG, "Subscribed.");
        if (purchase.isAutoRenewing()){
            return true;
        }

https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/util/Purchase.java