Not receiving result of Android In-App Purchase us

2019-07-21 11:29发布

I'm trying to work my way through the version 3 API of Android In-App Billing and have come across the following issue:

If I start a purchase with launchPurchaseFlow, it displays the purchase dialog as expected and let's me complete the purchase. After the purchase is completed, the product is then reported as owned if I request the SKU details again. So the purchase process itself works as expected.

What doesn't work are the notifications of the purchase itself. The OnIabPurchaseFinishedListener that I pass to launchPurchaseFlow never gets called. Neither if I cancel the purchase nor if I complete it.

I turned on logging for the IabHelper class and get the following output in LogCat:

IabHelper: Starting in-app billing setup.
IabHelper: Billing service connected.
IabHelper: Checking for in-app billing 3 support.
IabHelper: In-app billing version 3 supported for <my app>
IabHelper: Subscriptions AVAILABLE.

IabHelper: Starting async operation: refresh inventory
IabHelper: Querying owned items, item type: inapp
IabHelper: Package name: <my app>
IabHelper: Calling getPurchases with continuation token: null
IabHelper: Owned items response: 0
IabHelper: Continuation token: null
IabHelper: Querying SKU details.
IabHelper: Got sku details: <my test product>
IabHelper: Querying owned items, item type: subs
IabHelper: Package name: <my app>
IabHelper: Calling getPurchases with continuation token: null
IabHelper: Owned items response: 0
IabHelper: Continuation token: null
IabHelper: Querying SKU details.
IabHelper: Ending async operation: refresh inventory

IabHelper: Starting async operation: launchPurchaseFlow
IabHelper: Constructing buy intent for <my test product>, item type: inapp
IabHelper: Launching buy intent for <my test product>. Request code: 1

And that's where it ends.

My activity receives an onPause() event when the purchase dialog is shown and an onResume() event when the dialog goes away again (both, if the purchase is cancelled or completed). Both of these events currently do nothing in my app other than a System.out.println(...).

Is this a bug in the API? Or is there something I need to set up first? Any help would be appreciated.

1条回答
干净又极端
2楼-- · 2019-07-21 12:12

Digging through the source for IabHelper, I found the problem:

The result of the purchasing intent will be sent to the Activity's onActivityResult(...) method. From there, it needs to be manually forwarded to the IabHelper's handleActivityResult(...) method with something like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (myIabHelper.handleActivityResult(requestCode, resultCode, data)) return;
    super.onActivityResult(requestCode, resultCode, data);
}

This fixes the issue, both for successful and aborted purchases.

"Yay!" for comprehensive documentation...

查看更多
登录 后发表回答