从另一个活动查询IAP库存

时间:2013-10-08 20:10:59

标签: java android in-app-purchase in-app-billing android-billing

我的应用程序我正在使用In App Purchases升级到premium,我有一个Activity PremiumPurchase.java,从这里我可以查看高级功能并进行购买

IAP部分看起来像这样

//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.");
           }
       };

这用于查询购买mHelper.queryInventoryAsync(mGotInventoryListener); 我可以从这个活动查询购买,但我想从另一个活动查询它,我该怎么办?

0 个答案:

没有答案