Android 应用内计费消耗品

Android 应用内计费消耗品,android,android-studio,in-app-purchase,in-app-billing,Android,Android Studio,In App Purchase,In App Billing,我正在尝试为销售消耗品(硬币)实施谷歌应用内计费。我用非消耗品测试了一下,效果很好。但我不能让它消耗品。每次我测试它,我只能买一次! 这是我的密码: 公共类MainActivity扩展了AppCompatActivity{ 艾伯曼帮助者 boolean verifyDeveloperPayload(Purchase p) { String payload = p.getDeveloperPayload(); /* * TODO: verify that the developer p

我正在尝试为销售消耗品(硬币)实施谷歌应用内计费。我用非消耗品测试了一下,效果很好。但我不能让它消耗品。每次我测试它,我只能买一次! 这是我的密码:

公共类MainActivity扩展了AppCompatActivity{ 艾伯曼帮助者

boolean verifyDeveloperPayload(Purchase p) {
    String payload = p.getDeveloperPayload();

/*
 * TODO: verify that the developer payload of the purchase is correct. It will be
 * the same one that you sent when initiating the purchase.
 *
 * WARNING: Locally generating a random string when starting a purchase and
 * verifying it here might seem like a good approach, but this will fail in the
 * case where the user purchases an item on one device and then uses your app on
 * a different device, because on the other device you will not have access to the
 * random string you originally generated.
 *
 * So a good developer payload has these characteristics:
 *
 * 1. If two different users purchase an item, the payload is different between them,
 *    so that one user's purchase can't be replayed to another user.
 *
 * 2. The payload must be such that you can verify it even when the app wasn't the
 *    one who initiated the purchase flow (so that items purchased by the user on
 *    one device work on other devices owned by the user).
 *
 * Using your own server to store and verify developer payloads across app
 * installations is recommended.
 */

    return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlJQQTbdM6zP0585Ar0YKZAYQish29+AkZpdu4fGUO3WLoVm9UPOSNMMBmo8odzQbcVZdlKUfocohg/52qoQk9crVIhdHJM+O1GK9+hJSdVkZo0PWW5+1sJSCQ7cw0NTxIdDQVSYT0WWC2zkn8Fpxyz1N9pGHh21jxbviDYcnh1gyK+mCLt6jWcVxKl8BYgC0SS7K9F+7kHy+B/GG8ZSl2xhcCqlid/8cEjqH7yvMPciWA8lHvHB7rGz/nUg/v2ydhmUY6f8Ifh6+ygUu2XrhDU0v8wZ24yKw2Kw4SVZbm5ZmC/DXCgx+hIWVL+/yAFqHJ0ygqwW4aLTuKV6PyDaC1QIDAQAB";

    // compute your public key and store it in base64EncodedPublicKey
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.enableDebugLogging(true);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh no, there was a problem.
                Log.d("TAG", "Problem setting up In-app Billing: " + result);
                Log.i("TAG", "ERROR");
            }
            // Hooray, IAB is fully set up!
            //check owned items & consum em
            checkOwnedItems();
            //make a test purchase
                makePurchase();
        }
    });

}

private void makePurchase() {
    try {
        mHelper.launchPurchaseFlow(this, "next", 10001,
                mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
    } catch (IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
        showToast("oh no error purchase" + String.valueOf(e));
    }
}

private void checkOwnedItems() {
    try {
        mHelper.queryInventoryAsync(mGotInventoryListener);
    } catch (IabHelper.IabAsyncInProgressException e) {
        showToast("Oh no error in check()");
        //complain("Error querying inventory. Another async operation in progress.");
    }
}


// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Purchase item = inventory.getPurchase("next");
        if (item != null && verifyDeveloperPayload(item)) {
            //Log.d("TAG", "We have gas. Consuming it.");
            try {
                mHelper.consumeAsync(inventory.getPurchase("next"), mConsumeFinishedListener);
            } catch (IabHelper.IabAsyncInProgressException e) {
                // complain("Error consuming gas. Another async operation in progress.");
                showToast("oh no error when consuming");
            }
            return;
        }
    }

};


// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        Log.d("TAG", "Consumption finished. Purchase: " + purchase + ", result: " + result);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        // We know this is the "gas" sku because it's the only one we consume,
        // so we don't check which sku was consumed. If you have more than one
        // sku, you probably should check...
        if (result.isSuccess()) {
            // successfully consumed, so we apply the effects of the item in our
            // game world's logic, which in our case means filling the gas tank a bit
            //Log.d(TAG, "Consumption successful. Provisioning.");
            //mTank = mTank == TANK_MAX ? TANK_MAX : mTank + 1;
            // saveData();
            // alert("You filled 1/4 tank. Your tank is now " + String.valueOf(mTank) + "/4 full!");
        }
        else {
            //complain("Error while consuming: " + result);
        }
        Log.d("TAG", "End consumption flow.");
    }
};


// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d("TAG", "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        if (result.isFailure()) {
            // complain("Error purchasing: " + result);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            // complain("Error purchasing. Authenticity verification failed.");
            return;
        }

        Log.d("TAG", "Purchase successful.");

        if (purchase.getSku().equals("next")) {

            Log.d("TAG", "Purchase is gas. Starting gas consumption.");
            try {
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
            } catch (IabHelper.IabAsyncInProgressException e) {
                //complain("Error consuming gas. Another async operation in progress.");
                showToast("oh no error when consuming");
                return;
            }
        }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) try {
        mHelper.dispose();
    } catch (IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
    }
    mHelper = null;
}
private void showToast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
} }


对不起,我的英语不好,谢谢。

我在使用本机应用内计费系统时也遇到了问题,但后来我发现


希望有帮助。

我现在明白了:所以我的第一个问题是,我在CheckowneEdItems方法-->引发异常后立即调用了makePurchase:无法启动异步操作(launchPurchaseFlow),因为另一个异步操作(刷新库存)正在进行中。然后我使用按钮调用makePurchase,但它仍然不起作用。。。因为我在所有的听众中都用success或fail+exception祝酒,我知道没有调用onIabPurchaseFinished方法,所以我在google上找到了这个答案:

感谢您的快速回答,我会检查它。我不知道我做错了什么,但它仍然不起作用:它使onBillingInitialized干杯,但什么也没有发生(我将项目中的代码粘贴在onClick:case:consume按钮的onCreate方法中…)你希望看到google play对话框来实际购买东西吗?是的(我有一个测试帐户,非消耗品工作正常)要测试它是否真的有效,你需要发布应用程序,你可以在封闭测试版中发布,并请朋友测试它。你确定你成功地消费了你的物品吗?你确定没有异常吗?例如,
mGotInventoryListener
中的
checkOwnedItems()
mHelper.ConsumeSync()
都有空的catch块(糟糕的做法,顺便说一句),因此问题可能对您隐藏……是的,您说得对,如果成功或失败,我将举杯庆祝。(当时是凌晨2点,我忘了;)有什么例外?(顺便说一句,您是否在这些catch块中添加了e.printStackTrace()(我在上面的代码中没有看到它被更新)好,因此采购抛出错误:无法启动异步操作(launchPurchaseFlow),因为另一个异步操作(刷新库存)正在进行。但是我如何知道何时完成