Skip to content Skip to sidebar Skip to footer

Android In-app Billing, Non Consumable Items

I'm implementing in-app billing where the user shall be able to buy access to premium content. This is typical non-consumable items. (Let's say the premium content is extra questio

Solution 1:

If you don't want consume, then don't use consumeAsync.

 @Override
public void onQueryInventoryFinished(IabResult result, Inventory inv)
{
    if (result.isFailure())
    {
        Log.e(TAG, "In-app Billing query failed: " + result);
        return;
    } else
    {
        boolean hasPurchased_ITEM_SKU_PURCHASE_1 = inv.hasPurchase(ITEM_SKU_PURCHASE_1);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(KEY_PREF_PURCHASE_1_AVAILABLE, !hasPurchased_ITEM_SKU_PURCHASE_1);
        editor.commit();

        // You can update your UI here, ie. Buy buttons.
    }
}

You can use the sharedpref to store the purchase info, and also check every onCreate of the activity and update the sharedpref accordingly. The key part on how to check if a SKU is purchased is:

     boolean hasPurchased_ITEM_SKU_PURCHASE_1 = inv.hasPurchase(ITEM_SKU_PURCHASE_1);

Do your query sync in your IAP setup and update your UI accordingly.

 mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) 
    {
        if (!result.isSuccess()) {
            Log.d(TAG, "In-app Billing setup failed: " + 
                    result);
        } else {             
              mHelper.queryInventoryAsync(mReceivedInventoryListener);
            Log.d(TAG, "In-app Billing is set up OK");
        }
    }
 });

Post a Comment for "Android In-app Billing, Non Consumable Items"