简体   繁体   English

Android应用内购买-如何多次购买单个应用内购买?

[英]Android in-app-billing - How to buy a single in-app-purchase multiple times?

How can I buy one thing multiple times? 如何多次购买一件东西? Here is my code: 这是我的代码:

  // [...]
  String base64EncodedPublicKey =
                "MIIB...;

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new
                                   IabHelper.OnIabSetupFinishedListener() {
                                       public void onIabSetupFinished(IabResult result) {
                                           if (!result.isSuccess()) {
                                               Toast.makeText(gift.this, "Setup no Success", Toast.LENGTH_SHORT).show();
                                               Log.d(TAG, "In-app Billing setup failed: " + result);
                                           } else {
                                               Toast.makeText(gift.this, "Setup Success", Toast.LENGTH_SHORT).show();
                                           }
                                       }
                                   });


    }


    public void insert(View view) {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                mPurchaseFinishedListener, "mypurchasetoken");
    }

    @Override
    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()) {
                Toast.makeText(gift.this, "Error 1", Toast.LENGTH_SHORT).show();
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                Toast.makeText(gift.this, "OK 1", Toast.LENGTH_SHORT).show();
               // mHelper.consumeAsync(purchase, mConsumeFinishedListener);
            }

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

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

            if (result.isFailure()) {
                Toast.makeText(gift.this, "Error 2", Toast.LENGTH_SHORT).show();
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
                Toast.makeText(gift.this, "OK 2", Toast.LENGTH_SHORT).show();
            }
        }
    };
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        Toast.makeText(gift.this, "Success 1", Toast.LENGTH_SHORT).show();
                       // mHelper.consumeAsync(purchase, mConsumeFinishedListener);
                    } else {
                        Toast.makeText(gift.this, "Error 3", Toast.LENGTH_SHORT).show();
                    }
                }
            };

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

When I click on my button it says: "payment successful" and after that when I click on my button again it does not do anything. 当我单击我的按钮时,它说:“付款成功”,此后再次单击我的按钮时,它什么也不做。

I have 2 questions: 我有两个问题:

  1. How to buy a single in-app-purchase multiple times? 如何多次购买一个应用程序内购买?
  2. How can I display a Toast after the payment was successful? 付款成功后如何显示Toast?

Thanks for answers. 感谢您的回答。

  1. The purchase must be consumed after every purchase, then it can be purchased again. 每次购买后必须消耗掉所购买的物品,然后才能再次购买。 If you are able to purchase the item once but not again, then almost assuredly the problem is that you are not consuming it correctly. 如果您能够一次购买但不能再次购买,那么几乎可以肯定的问题是您没有正确使用它。

  2. There doesn't appear to be any problem with your code, Toast.makeText(...) should work fine. 您的代码似乎没有任何问题,Toast.makeText(...)应该可以正常工作。

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

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