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

时间:2017-01-11 14:28:31

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

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

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

当我点击我的按钮时,它显示:“付款成功”,之后当我再次点击我的按钮时,它什么也没做。

我有两个问题:

  1. 如何多次购买单个应用内购买?
  2. 付款成功后如何显示Toast?
  3. 感谢您的回答。

1 个答案:

答案 0 :(得分:1)

  1. 每次购买后都必须购买,然后再购买。如果您能够购买该商品一次但不能再购买,那么几乎可以肯定的是,您没有正确使用该商品。

  2. 您的代码似乎没有任何问题,Toast.makeText(...)应该可以正常工作。