1

我正在测试我的 Android 应用程序并面临“无法购买商品(响应:7:商品已拥有)”。该项目是“托管应用内产品”类型。我做了一些研究,发现了这个(http://developer.android.com/google/play/billing/api.html#managed):

Managed in-app products are items that have their ownership information tracked and managed
by Google Play. When a user purchases a managed in-app item, Google Play stores the
purchase information for each item on a per-user basis. This enables you to later query
Google Play at any time to restore the state of the items a specific user has purchased.
This information is persistent on the Google Play servers even if the user uninstalls the
application or if they change devices.

If you are using the Version 3 API, you can also consume managed items within your
application. You would typically implement consumption for items that can be purchased
multiple times (such as in-game currency, fuel, or magic spells). Once purchased, a managed
item cannot be purchased again until you consume the item, by sending a consumption request
to Google Play. To learn more about in-app product consumption, see Consuming Items.

...

You can use the consumption mechanism to track the user's ownership of in-app products.

In Version 3, all in-app products are managed. This means that the user's ownership of all
in-app item purchases is maintained by Google Play, and your application can query the
user's purchase information when needed. When the user successfully purchases an in-app
product, that purchase is recorded in Google Play. Once an in-app product is purchased, it
is considered to be "owned". In-app products in the "owned" state cannot be purchased from
Google Play. You must send a consumption request for the "owned" in-app product before
Google Play makes it available for purchase again. Consuming the in-app product reverts it
to the "unowned" state, and discards the previous purchase data.

如何在代号一中“发送消费请求”?

4

2 回答 2

3

假设您使用IabHelper.java的是 google 在其 android 中提供的SDK。以下是路径:

Android_SDK\sdk\extras\google\play_billing\samples\TrivialDrive\src\com\example\android\trivialdrivesample\util

下面是初始化应用内计费、查询 google play 以获取用户购买和消费购买的代码。

  1. 初始化应用内计费。

    /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY
       (that you got from the Google Play developer console). 
    */
    IabHelper mHelper = new IabHelper(this, base64EncodedPublicKey);
    
    // Check whether in-app billing is supported or not.
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
    {
        @Override
        public void onIabSetupFinished(IabResult result)
        {
            if (result.isSuccess())
            {
                // In-app billing is supported. Now, queryInventoryAsync to whether user already purchased.
                ArrayList<String> productIDs = new ArrayList<String>();
                // ITEM_SKU -> Name of the billing item in google play developer console
                productIDs.add(ITEM_SKU);
                mHelper.queryInventoryAsync(true, productIDs, mReceivedInventoryListener);
            }
            else
            {
                // Billing not supported by the device
            }
        }
    });
    
  2. 查询用户购买

    mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener()
    {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory)
        {
            if (result.isSuccess())
            {
                // Will be true if already purchased
                boolean hasPurchase = inventory.hasPurchase(ITEM_SKU);
    
                // Here enable or disable your app features
                ...
    
                    /* How to consume the purchase */
    
                // First, get the Purchase from inventory
                Purchase purchase = inventory.getPurchase(ITEM_SKU);
    
                // Consume the purchase
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
    
            }
            else
            {
                // Failed to query google play for purchases.
            }
        }
    };
    
  3. 购买消费的监听器

    mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener()
    {
    
        @Override
        public void onConsumeFinished(Purchase purchase, IabResult result)
        {
            if (result.isSuccess())
            {
                // Purchase consumed.
            }
            else
            {
                // Purchase not consumed.
            }
        }
    };
    
于 2016-02-17T08:49:42.837 回答
1

使用

Purchase.getInAppPurchase().wasPurchased(String sku)

如果该项目是托管项目并且已经购买,它将返回 true

于 2016-02-17T15:40:37.457 回答