简体   繁体   English

消耗品的Android应用内购买

[英]Android in-app purchase for consumable items

The problem is that the app doesn't allow the users to purchase more than once. 问题在于该应用不允许用户多次购买。 I need to make my item consumable, and the answer is all explained here . 我需要使我的物品可消耗,答案在这里得到了解释。

What I need to know is EXACTLY WHERE I have to put this part: 我需要知道的是,我必须把这部分放在哪里:

mHelper.consumeAsync(purchase, mConsumeFinishedListener);

in my code? 在我的代码中?

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


        });

    }

}

Because in the other answer Haxis says "you have to call the consume function just after the purchase." 因为在另一个答案中, Haxis说:“您必须在购买后立即调用消费函数。”

Could anyone help me please? 有人可以帮我吗?

As you can check in the google sample - https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java 正如您可以签入Google示例-https: //github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java

consumeAsync() is called twice: abuseAsync()被调用两次:

-When the purchase is complete, in OnIabPurchaseFinishedListener -购买完成后,在OnIabPurchaseFinishedListener

-When the inventory is queried at the start of the activity, in QueryInventoryFinishedListener -在活动开始时查询库存时,请在QueryInventoryFinishedListener

You also need to call it when the purchase was successful as well as when you query the inventory and find a consumable item in there (because if the item is consumed, then it won't appear in the inventory) 您还需要在购买成功以及查询库存并在其中找到消耗品时调用它(因为如果消耗了该物料,则它将不会出现在库存中)

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

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