简体   繁体   English

Android应用内购买消费错误

[英]Android in app purchase consuming error

i have almost finished my first app. 我已经快完成我的第一个应用程序了。 I have my in-app purchasing working, but it turns out i have a problem with consuming the purchases, since it should be possible to buy the item endless times. 我有我的应用程序内购买功能,但事实证明我在消费商品时遇到了问题,因为应该可以无休止地购买商品。 I think i have edited the code to consume the purchased item correctly now, I will provide the code so you can correct me if I'm wrong. 我认为我已经编辑了代码以正确使用现在购买的物品,我将提供代码,以便在我输入错误时纠正我的情况。 But even though this code might be correct now, I can't purchase the product on my test device again, since it wasn't consumed when bought in the first place. 但是,即使该代码现在可能正确无误,我也无法在测试设备上再次购买该产品,因为最初购买该产品时并未消耗。 How can i consume the product now? 我现在如何消费该产品? Here is my updated code. 这是我更新的代码。

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

I figured out the answer. 我想出了答案。 I had to change the startSetup method so it queries the inventory right away. 我必须更改startSetup方法,以便它立即查询库存。 Here is my new code. 这是我的新代码。

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

Here I check if the hints have been purchased before consuming it. 在这里,我在使用前检查提示是否已购买。 Otherwise i would receive an error. 否则我会收到一个错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM