0

Im my app i am using In App Purchases to upgrade to premium, i have an Activity PremiumPurchase.java, from here i can view the premium features and make a purchase

the IAP part looks like this

//PREMIUM UPGRADE-----------------------------------------------------------------------------------------

    // User clicked the "Upgrade to Premium" button.
       public void onUpgradeAppButtonClicked(View arg0) {
           Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade.");
      //     setWaitScreen(true);
           if (!isPurchasing) {
               mHelper.queryInventoryAsync(mGotInventoryListener);
               isPurchasing = true;
               showLoading();
           }

         //  mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener);


       }
       private void startPremiumPurchase(){
           Log.d(TAG, "Starting Premium purchase");
           mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener);

       }


       ServiceConnection mServiceConn = new ServiceConnection() {
           @Override
           public void onServiceDisconnected(ComponentName name) {
               mService = null;
           }

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

               ArrayList skuList = new ArrayList();
               skuList.add(SKU_PREMIUM);
               Bundle querySkus = new Bundle();
               querySkus.putStringArrayList("ITEM_ID_LIST", skuList);


                Bundle skuDetails;
                try {
                    skuDetails = mService.getSkuDetails(3, 
                               getPackageName(), "inapp", querySkus);


                int response = skuDetails.getInt("RESPONSE_CODE");
                if (response == 0) {
                   ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");

                   for (String thisResponse : responseList) {
                      JSONObject object = new JSONObject(thisResponse);
                      String sku = object.getString("productId");
                      String price = object.getString("price");
                      if (sku.equals(SKU_PREMIUM)) premiumUpgradePrice = price;
                      if (!isPremium) {
                        CharSequence premiumBtnTxt = premiumBtn.getText();
                         premiumBtn.setText(premiumBtnTxt + " "+premiumUpgradePrice);
                    }
                   }
                }
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }


           }
        };


    // Callback for when a purchase is finished
       IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
           public void onIabPurchaseFinished(IabResult result, Purchase purchase) {

               Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
               dialog.cancel();
               int duration = Toast.LENGTH_SHORT;
               if (result.isFailure()) {
                   Log.d(TAG, "WARNING problem with purchase");
                   // Oh noes!
                   return;
               }
               if (purchase.getSku().equals(SKU_PREMIUM)) {
                   Log.d(TAG, "Purchased premium upgrade. Congratulating user.");

                   isPremium = true;
                   savePrefs();

                   Toast.makeText(getBaseContext(), R.string.premium_upgrade_success, duration).show();
               }
           }
       };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            // not handled, so handle it ourselves (here's where you'd
            // perform any handling of activity results not related to in-app
            // billing...
            super.onActivityResult(requestCode, resultCode, data);
        }
        else {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
        }
    }


    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
           public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
               Log.d(TAG, "Query inventory finished.");

               if (result.isFailure()) {
                   Log.d(TAG, "Quering inventory failed."+ result);
                   dialog.cancel();
                   return;
               }

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


               // Do we have the premium upgrade?
                isPremium = inventory.hasPurchase(SKU_PREMIUM);

               Log.d(TAG, "User is " + (isPremium ? "PREMIUM" : "NOT PREMIUM"));
               savePrefs();

               isPurchasing = false;

               if (isPremium) {
                   Toast.makeText(getBaseContext(), R.string.already_premium, Toast.LENGTH_SHORT).show();
                   Log.d(TAG, "You're already premium");
                   dialog.cancel();
               }
               else {
                   startPremiumPurchase();
               }
               Log.d(TAG, "Initial inventory query finished; enabling main UI.");
           }
       };

This is used to query the purchases mHelper.queryInventoryAsync(mGotInventoryListener); I can query the purchase from this activity, but i would like to query it from another activity aswell, how can i do it?

Maulik
  • 3,316
  • 20
  • 31
DoubleP90
  • 1,249
  • 1
  • 16
  • 29
  • I think if you check every time in the different activity for the premium purchase every time activity start then all time user has to wait for the purchase status process, I think best way is you should go for the other way store data in database and check your database every time in other activity. check this link: http://stackoverflow.com/questions/19242939/android-in-app-purchase-how-to-check-if-user-have-purchased-one-item – Maulik Oct 12 '13 at 12:19
  • if you still go with your way then you can copy your code in your other activity and make same purchase with the same product id, google play doesn't depends on your activity. – Maulik Oct 12 '13 at 12:21
  • One more link I found for different activity: http://stackoverflow.com/questions/11076038/in-app-billing-switching-activities/11076842#11076842 hope it will help to solve your problem. – Maulik Oct 14 '13 at 14:01

0 Answers0