如何多次购买一件东西?这是我的代码:

  // [...]
  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;
    }
}


当我单击我的按钮时,它说:“付款成功”,此后再次单击我的按钮时,它什么也不做。

我有两个问题:


如何多次购买一个应用程序内购买?
付款成功后如何显示Toast?


感谢您的回答。

最佳答案

每次购买后必须消耗掉所购买的物品,然后才能再次购买。如果您能够一次购买但不能再次购买,那么几乎可以肯定的问题是您没有正确使用它。
您的代码似乎没有任何问题,Toast.makeText(...)应该可以正常工作。

10-07 20:00
查看更多