应用内购买成功,然而,它没有运行我的代码。为什么?

时间:2017-07-05 23:31:26

标签: android in-app-purchase

我是应用内购买系统的新手。我刚看了一些教程,然后实现了应​​用内购买。但是,当我的应用内购买成功时,它没有运行我的代码。是否必须运行以下代码?:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001) {
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");

        if (resultCode == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String sku = jo.getString(inappid);
                Toast.makeText(
                        MainActivity.this,
                        "You have bought the " + sku
                                + ". Excellent choice,adventurer!",
                        Toast.LENGTH_LONG).show();
                writeInApp("1");
                playGroundMain.bonus_pop_up(R.drawable.rich_big, "\"VIP Монстр\"");
                playGroundMain.reWriteBonus("1", 6);

            } catch (JSONException e) {
                System.out.println("Failed to parse purchase data.");
                e.printStackTrace();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我已经使用付款系统制作了一些应用程序,你应该看看这个听众

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

            Log.d(TAG, "Consumption finished. JSON: " + purchase.getOriginalJson() + ", signature: " + purchase.getSignature());

            if (mHelper == null) return;

            if (result.isSuccess())
            {
                    Log.i(TAG, "BOUGHT THE ITEM :-)")
            }
            else
            {
                Log.e(TAG, "Error while consuming: " + result);
            }
            Log.d(TAG, "End consumption flow.");
        }
    };

你可以在IabHelper的consumeAsync函数中添加这个监听器,如下所示:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() 
{
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
        {

            if (purchase.getSku().equals(SKU_21500))
            {
                // bought 21500 credits
                Log.d(TAG, "Purchase is 21500 credits. Starting credits consumption.");
                try
                {
                    mHelper.consumeAsync(purchase, mConsumeFinishedListener);
                }
                catch (IabHelper.IabAsyncInProgressException e)
                {
                    Log.d(TAG, "Error consuming 21500 credits. Another async operation in progress.");
                    return;
                }
            }
        }
}

然后你可以把" mPurchaseFinishedListener"在" launchPurchaseFlow"功能如此:

mHelper.launchPurchaseFlow(activity, item, REQUEST_CODE, mPurchaseFinishedListener);

REQUEST_CODE为10001

希望这有帮助!

答案 1 :(得分:0)

如果您尝试购买非消费品,请购买代码即可。

在进行测试购买时,有时不会调用onActivityResult()。在实际购买中,这可能不会发生。

在这种情况下,为了处理购买,你必须检查result code == user already owns this item //resultCode 7,这样用户就不必在这种情况下再次付款。