2

I am able to successfully purchase in-app billing items in my app, but I have not yet been able to successfully check which items the user has purchased, as I am getting a a null pointer exception on this line:

  ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

I did see the other posts on this topic, but those solutions either did not work in my case, or I don't fully understand the solution:

Here is the entire method:

private void checkOwnedItems() throws RemoteException {

    Bundle ownedItems;
    String sku = "";

    ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

    int response = ownedItems.getInt("RESPONSE_CODE");
    if (response == 0) {
        ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
        ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");

        if (purchaseDataList.size() > 0) {
            //user owns 1 or more items

            for (int i = 0; i < purchaseDataList.size(); ++i) {
                sku = ownedSkus.get(i);
            }
            Toast.makeText(SettingsActivity.this, "You own these features: " + sku, Toast.LENGTH_LONG).show();
        } else {
            //user owns zero items, launch purchase flow
            MACAddress = UniqueID.getMACAddress("wlan0");
            int requestCode = 22222;
            mHelper.launchPurchaseFlow(SettingsActivity.this, productID, requestCode, mPurchaseFinishedListener, MACAddress);

        }
    }
}

I ran the debugger, and it appears that mService is null. Where and how am I supposed to initialize mService?

Currently I am trying to initialize mService in the onServiceConnected method, but perhaps onServiceConnected is never getting called. When and how should I be calling the onServiceConnected method?

    ServiceConnection mServiceConn = new ServiceConnection()

{
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }
};
Community
  • 1
  • 1
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
  • FYI I am trying to follow the instructions on the Google Developer documentation here, under the section "Querying for Items Available for Purchase" http://developer.android.com/google/play/billing/billing_integrate.html If you want me to post anymore code from the app, or answer any more questions, I am happy to do so. – joshgoldeneagle Dec 03 '15 at 03:11

1 Answers1

0

I am able to successfully purchase in-app billing items in my app, but I have not yet been able to successfully check which items the user has purchased

You should use the Inventory instance to get the list of ownedItems purchased by the current user. For example, on app start I'm checking whether the user has purchased My_APP_DEMO_PRODUCT or not and based on that I'm changing the UI of the screen. For the above use case, I'm using QueryInventoryFinishedListener .

Note The following code is taken from the Google's IAB sample project.

// 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) {
         Log.d(DEBUG_TAG, "Query inventory finished.");

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

         // Is it a failure?
         if (result.isFailure()) {
             complain("Failed to query inventory: " + result);
             return;
         }

         Log.d(DEBUG_TAG, "Query inventory was successful.");

         /*
          * Check for items we own. Notice that for each purchase, we check
          * the developer payload to see if it's correct! See
          * verifyDeveloperPayload().
          */

         // Do we have the My_APP_DEMO_PRODUCT purchase.
         Purchase premiumPurchase = inventory.getPurchase(My_APP_DEMO_PRODUCT
);
         boolean isPurchased = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
         if (isPurchased) {
             Log.d(DEBUG_TAG, “User had purchased this My_APP_DEMO_PRODUCT product”);


             // Do whatever you want
             changeAppUI();
         }


         Log.d(DEBUG_TAG, "Initial inventory query finished; enabling main UI.");
     }
 };

And in Activity's onCreate() method,

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d(DEBUG_TAG, "Setup finished.");

                    if (!result.isSuccess()) {
                        // Oh noes, there was a problem.
                        complain("Problem setting up in-app billing: " + result);
                        return;
                    }

                    if (mHelper == null) return;

                    // IAB is fully set up. Now, let's get an inventory of stuff we own.
                    Log.d(DEBUG_TAG, "Setup successful. Querying inventory.");
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                }
            });  
Rahul Chaurasia
  • 1,601
  • 2
  • 18
  • 37
  • So far I noticed I was forgetting to call mHelper.queryInventoryAsync(mGotInventoryListener) in mHelper.startSetup method. – joshgoldeneagle Dec 03 '15 at 18:33
  • One other issue so far, I do not have the method verifyDeveloperPayload defined anywhere. It looks like it is supposed to be in the Purchase class. Guess I need to find this code and define the method. – joshgoldeneagle Dec 03 '15 at 18:34
  • Ok Rahul, I think your answer working! I had to replace QueryInventoryAsync methods in IABHelper.java with the code from the TrivialDrive sample, and declare the mGotInventoryListener variable as well. I tested on two different Android devices so far. The only thing I have to test still is to cancel my purchases, and see what happens when user is purchasing for the first time. It's looking promising! – joshgoldeneagle Dec 03 '15 at 20:38
  • @joshgoldeneagle Good to know that it helped you – Rahul Chaurasia Dec 04 '15 at 14:07