Android在应用内购买消费错误

时间:2014-03-22 23:29:57

标签: android in-app-purchase

我差不多完成了我的第一个应用程序。我有我的应用内购买工作,但事实证明我有消费购买的问题,因为它应该可以无限次购买该项目。我想我已经编辑了代码以正确使用所购买的商品,我会提供代码,这样如果我错了就可以纠正我。但即使此代码现在可能是正确的,我也无法再次在我的测试设备上购买该产品,因为它首先购买时并未消耗。我现在该如何消费产品?这是我更新的代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d("Unlock", "Error purchasing: " + result);
            return;
        } else if (purchase.getSku().equals(SKU_HINTS)) {
            consumeItem();              
        }

    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
        } else {
            mHelper.consumeAsync(inventory.getPurchase(SKU_HINTS),
                    mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {

        if (result.isSuccess()) {
            add_hints();
            purchase1.setText("Purchase complete");
        } else {
            // handle error
        }
    }
};

public void onClick(View v) {
case R.id.bUnlockHints:
        mHelper.launchPurchaseFlow(this, SKU_HINTS, 10001,
                mPurchaseFinishedListener, "");
        break;
 }

1 个答案:

答案 0 :(得分:0)

我想出了答案。我不得不更改startSetup方法,以便立即查询库存。这是我的新代码。

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
        } 
            // This is the updated part
        Purchase hintPurchase = inventory.getPurchase(SKU_HINTS);

        if (hintPurchase != null) {
            mHelper.consumeAsync(inventory.getPurchase(SKU_HINTS),
                    mConsumeFinishedListener);
        }
    }
};

在此,我会在消费之前检查是否已购买提示。否则我会收到错误。

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d("Ultimate Quiz",
                        "Problem setting up In-app Billing: " + result);
            }
            // Hooray, IAB is fully set up!
            // Here I run the queryInventory to check right away if the product has been bought, and consume it if it has.
            mHelper.queryInventoryAsync(mReceivedInventoryListener);
        }
    });