In my application, the user can buy the removal of ads, I save this item (without consumption). So I have a snippet in my main action that checks if the user is buying an item.
public class BillingInventoryFragment extends Fragment { // Helper billing object private IabHelper mHelper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); initialiseBilling(); } private void initialiseBilling() { if (mHelper != null) { return; } // Create the helper, passing it our context and the public key to verify signatures with mHelper = new IabHelper(getActivity(), BillingUtils.getApplicationKey()); // Enable debug logging (for a production application, you should set this to false). mHelper.enableDebugLogging(true); // Start setup. This is asynchronous and the specified listener will be called once setup completes. mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { @Override public void onIabSetupFinished(IabResult result) { // Have we been disposed of in the meantime? If so, quit. if (mHelper == null) { return; } // Something went wrong if (!result.isSuccess()) { Log.e(getActivity().getApplicationInfo().name, "Problem setting up in-app billing: " + result.getMessage()); return; } // IAB is fully set up. Now, let get an inventory of stuff we own. mHelper.queryInventoryAsync(iabInventoryListener()); } }); } /** * Listener that called when we finish querying the items and subscriptions we own */ private IabHelper.QueryInventoryFinishedListener iabInventoryListener() { return new IabHelper.QueryInventoryFinishedListener() { @Override public void onQueryInventoryFinished(IabResult result, Inventory inventory) { // Have we been disposed of in the meantime? If so, quit. if (mHelper == null) { return; } // Something went wrong if (!result.isSuccess()) { Log.d(TAG, String.format("result not success, result = %s", result) ); return; } // Do your checks here... // Do we have the premium upgrade? Purchase purchasePro = inventory.getPurchase(BillingUtils.SKU_PRO); // Where BillingUtils.SKU_PRO is your product ID (eg. permanent.ad_removal) Log.d(TAG, String.format("Purchase pro = %s", purchasePro)); BillingUtils.isPro = (purchasePro != null && BillingUtils.verifyDeveloperPayload(purchasePro)); // After checking inventory, re-jig stuff which the user can access now // that we've determined what they've purchased BillingUtils.initialiseStuff(); } }; } /** * Very important! */ @Override public void onDestroy() { super.onDestroy(); if (mHelper != null) { mHelper.dispose(); mHelper = null; } }
}
Everything works on one device, but when I tested it on a second device:
inventory.getPurchase(BillingUtils.SKU_PRO);
returns null.
When I try to buy an item again on this second device, I cannot, because I own it.
source share