How to query (price) information for multiple in-app products at once?

前端 未结 2 1701
灰色年华
灰色年华 2021-02-02 11:48

In Android in app billing is it possible to somehow query (price) information for all products using one query? Optimally you could pass in the product IDs and it would return i

相关标签:
2条回答
  • 2021-02-02 12:38

    It is possible now with Billing API v3. You can get information with getSkuDetails() method. Example is here.

    ArrayList skuList = new ArrayList();
    skuList.add("premiumUpgrade"); 
    skuList.add("gas");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
    
    Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
    
    int response = skuDetails.getInt("RESPONSE_CODE");
    if (response == 0) {
        ArrayList 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("premiumUpgrade")) {
                mPremiumUpgradePrice = price;
            } else if (sku.equals("gas")) { 
                mGasPrice = price;
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 12:39

    The Google example snippets (like https://developer.android.com/google/play/billing/billing_integrate.html#QueryDetails) and other snippets I saw did not fit my usage. I either need to have an inventory instance or an mHelper (which is the IAB Helper). Looks like the question's upvote number is bigger than the accepted answer's upvote, so I'm not alone. My usage is similar to the TrivialDrive Example (https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/), so let's look at that.

    In the MainActivity onCreate there's a call for queryInventoryAsync:

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                Log.d(TAG, "Setup finished.");
                ...
    
                // IAB is fully set up. Now, let's get an inventory of stuff we own.
                Log.d(TAG, "Setup successful. Querying inventory.");
                try {
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                } catch (IabAsyncInProgressException e) {
                    complain("Error querying inventory. Another async operation in progress.");
                }
            }
        });
    

    In the listener you gonna have the inventory instance.

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

    My problem is that my in-app products are consumable, and I'd like to query the details for all of my products in the beginning whether the user already bought some or not. For example I want to know the price of all the products expressed in the local currency instead of just USD, so the user is informed before already engaging too much into the purchase. Normally queryInventory only gives back what the user already purchased / handled (either consumable or non consumable products, subscriptions, ...).

    If you are in the same shoes (want to know all details at the start) what you need to do is two fold:

    1. You need to call a polymorph variant of the queryInventoryAsync within the mHelper.startSetup where you can instruct.
    2. In the listener you'll be able to iterate over your SKUs and see the details.

    So the first:

    String[] SKUS = { SKU_1, SKU_2, SKU_3, SKU_4, SKU_5 };  // products
    ...
    mHelper.queryInventoryAsync(true, Arrays.asList(SKUS), new ArrayList<String>(), mGotInventoryListener);
    

    The second parameter here is the product SKUs, the third (I'm not using here) is the subscription SKUs.

    Second: Then within the QueryInventoryFinishedListener now you have all of them in the inventory and you can iterate over your SKUs:

            for (String sku: SKUS) {
                SkuDetails skuDetails = inventory.getSkuDetails(sku);
                String price = skuDetails.getPrice();
                ((android.widget.TextView)findViewById( *sku's ID* )).setText(price);
            }
    

    Hope this helps. Soon I need to upgrade to a non AIDL based solution.

    0 讨论(0)
提交回复
热议问题