Android应用内购买耗材

时间:2017-08-24 09:21:00

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

问题是该应用不允许用户多次购买。我需要让我的物品消耗品,答案全部解释为here

我需要知道的是我必须把这一部分放在哪里:

mHelper.consumeAsync(purchase, mConsumeFinishedListener);

在我的代码中?

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }

    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()) {
                // Handle error
                return;
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                buyButton.setEnabled(false);
            }

        }
    };





    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(ITEM_SKU),
                        mConsumeFinishedListener);


            }
        }
    };



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

                    if (result.isSuccess()) {
                        clickButton.setEnabled(true);

                    } else {
                        // handle error
                    }
                }
            };




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_in_app_billing);

        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton = (Button)findViewById(R.id.clickButton);



        String base64EncodedPublicKey =
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3F8aC2kUFQHf/X3xnfgulD0UpgrQifcjZ66zzhUPhQ/TcrONl22V74Q/Uj57rCwSUfdzz7wbUPxuPayGKBozzoH+2vhMGSetgCFZLcrNbRpBBbihOZrj//GTXMa6VkpUPTAqthEF0oI1M/bW9vF75xZI3u2KAS/AYDfqLTRZ6mh+xh6n/3i0ntSZT+UwzguwyHfS9JwuGGg5AKSutaWhnvOTNeQjsxTskc483h9DfvvRiwdiQPlv7wJRSSIc3RHVwDHleEJ8rsRa8JTypBJuL5oRZSGePUlejWhJvs23tgy5xrvGsMgsICssGzIem2XXSUWm/NDjeO0v2Eh+quQKVQIDAQAB";

        mHelper = new IabHelper(this, base64EncodedPublicKey);


        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
        {

            public void onIabSetupFinished(IabResult result)
            {
                if (!result.isSuccess())
                {
                    Log.d(TAG, "In-app Billing setup failed: " + result);
                } else
                {
                    Log.d(TAG, "In-app Billing is set up OK");
                }
            }


        });

    }

}

因为在other answer Haxis中说“你必须在购买后立即调用消费功能。”

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

您可以在google示例中查看 - https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java

consumeAsync()被调用两次:

- 购买完成后,在OnIabPurchaseFinishedListener

- 在活动开始时查询广告资源,在QueryInventoryFinishedListener

您还需要在购买成功时以及查询库存并在那里查找消耗品时调用它(因为如果物品被消耗,那么它不会出现在库存中)< / p>