The Google example snippets (like https://developer.android.com/google/play/billing/billing_integrate.html#QueryDetails) and other snippets I saw did not fit my usage. I either need to have an inventory
instance or an mHelper
(which is the IAB Helper). Looks like the question's upvote number is bigger than the accepted answer's upvote, so I'm not alone. My usage is similar to the TrivialDrive Example (https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/), so let's look at that.
In the MainActivity
onCreate
there's a call for queryInventoryAsync
:
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
...
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
try {
mHelper.queryInventoryAsync(mGotInventoryListener);
} catch (IabAsyncInProgressException e) {
complain("Error querying inventory. Another async operation in progress.");
}
}
});
In the listener you gonna have the inventory instance.
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
...
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
My problem is that my in-app products are consumable, and I'd like to query the details for all of my products in the beginning whether the user already bought some or not. For example I want to know the price of all the products expressed in the local currency instead of just USD, so the user is informed before already engaging too much into the purchase. Normally queryInventory
only gives back what the user already purchased / handled (either consumable or non consumable products, subscriptions, ...).
If you are in the same shoes (want to know all details at the start) what you need to do is two fold:
- You need to call a polymorph variant of the
queryInventoryAsync
within the mHelper.startSetup
where you can instruct.
- In the listener you'll be able to iterate over your SKUs and see the details.
So the first:
String[] SKUS = { SKU_1, SKU_2, SKU_3, SKU_4, SKU_5 }; // products
...
mHelper.queryInventoryAsync(true, Arrays.asList(SKUS), new ArrayList<String>(), mGotInventoryListener);
The second parameter here is the product SKUs, the third (I'm not using here) is the subscription SKUs.
Second: Then within the QueryInventoryFinishedListener
now you have all of them in the inventory
and you can iterate over your SKUs:
for (String sku: SKUS) {
SkuDetails skuDetails = inventory.getSkuDetails(sku);
String price = skuDetails.getPrice();
((android.widget.TextView)findViewById( *sku's ID* )).setText(price);
}
Hope this helps. Soon I need to upgrade to a non AIDL based solution.