如何一次查询多个应用内商品的(价格)信息?

发布于 2024-11-05 05:17:56 字数 470 浏览 1 评论 0原文

在 Android 应用程序计费中,是否可以使用一个查询以某种方式查询所有产品的(价格)信息?最好您可以传入产品 ID,它会返回这些信息。

我正在寻找的是 Android Market 的 SKProductsRequest 等效项。 http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKProductsRequest/Reference/Reference.html#//apple_ref/occ/cl/SKProductsRequest

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 information for those.

What I'm looking for is the SKProductsRequest equivalent for Android Market. http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKProductsRequest/Reference/Reference.html#//apple_ref/occ/cl/SKProductsRequest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-11-12 05:17:56

现在可以通过 Billing API v3 实现。您可以使用 getSkuDetails()< 获取信息/a> 方法。示例如下此处

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;
        }
    }
}

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;
        }
    }
}
愁以何悠 2024-11-12 05:17:56

Google 示例片段(例如 https://developer.android.com/ google/play/billing/billing_integrate.html#QueryDetails)和我看到的其他片段不适合我的用法。我需要一个 inventory 实例或一个 mHelper (即 IAB Helper)。看起来问题的赞成票数比接受的答案的赞成票数还要多,所以我并不孤单。我的用法与 TrivialDrive 示例 (https://github.com/TrivialDrive 示例类似)。 com/googlesamples/android-play-billing/blob/master/TrivialDrive/),让我们看一下。

MainActivity onCreate 中,有一个对 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.");
            }
        }
    });

在侦听器中,您将拥有库存实例。

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

我的问题是我的应用内产品是消耗品,我想在开始时查询我的所有产品的详细信息,无论用户是否已经购买了一些产品。例如,我想知道以当地货币而不是美元表示的所有产品的价格,以便用户在进行过多购买之前得到通知。通常 queryInventory 仅返回用户已购买/处理的内容(消费品或非消费品、订阅等)。

如果您也有同样的情况(想从一开始就知道所有详细信息),您需要做的有两件事:

  1. 您需要在 mHelper 中调用 queryInventoryAsync 的多态变体。 startSetup 您可以在其中进行指示。
  2. 在侦听器中,您将能够迭代 SKU 并查看详细信息。

所以第一个

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

这里的第二个参数是产品SKU,第三个参数(我在这里没有使用)是订阅SKU。

第二:然后在 QueryInventoryFinishedListener 中,现在您已将所有这些都包含在 inventory 中,并且您可以迭代 SKU:

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

希望这会有所帮助。很快我需要升级到非基于 AIDL 的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文