12

我已经成功地在我的应用程序中实施了应用程序计费,一切正常。我现在正在尝试检索项目的价格(在开发人员控制台中设置),以便我可以在我的应用程序中反映这些价格,而无需硬编码值。

这段代码很明显只收集已经通过库存购买的物品的价格,这不是我想要的:

SkuDetails gasDetails = inventory.getSkuDetails(SKU_FULL);      

            if (gasDetails != null){
                alert("Gas is " + gasDetails.getPrice());}

我看过文档查询可购买物品我认为 Helper 类会实现某种获取价格的方法。

所以,我的问题是:谁能指出我正确的方向?

4

3 回答 3

9

如果您使用 Google 在“TrivialDrive”示例中提出的实现,您可以通过在查询方法中将 true 传递给参数“details”和“moreSkus”来检索所有 sku 的信息(即使它们不是购买的)库存

/**
 * Queries the inventory. This will query all owned items from the server, as well as
 * information on additional skus, if specified. This method may block or take long to execute.
 * Do not call from a UI thread. For that, use the non-blocking version {@link #refreshInventoryAsync}.
 *
 * @param querySkuDetails if true, SKU details (price, description, etc) will be queried as well
 *     as purchase information.
 * @param moreItemSkus additional PRODUCT skus to query information on, regardless of ownership.
 *     Ignored if null or if querySkuDetails is false.
 * @param moreSubsSkus additional SUBSCRIPTIONS skus to query information on, regardless of ownership.
 *     Ignored if null or if querySkuDetails is false.
 * @throws IabException if a problem occurs while refreshing the inventory.
 */
public Inventory queryInventory(boolean querySkuDetails, List<String> moreItemSkus,
                                    List<String> moreSubsSkus) throws IabException {
于 2013-12-12T12:55:53.720 回答
5

好的,我找到了解决方案。我已经破译了开发人员文档,并且其中似乎存在错误。

这是我在 IabHelper 中创建的解决方案:

public String getPricesDev(String packageName) throws RemoteException, JSONException{


        ArrayList<String> skuList = new ArrayList<String>();
        skuList.add("full.discount.fetch");
        skuList.add("gas");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);

    Bundle skuDetails = mService.getSkuDetails(3,packageName, "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.contains("full.discount.fetch")) return price;

       }
    } 
    return "Not found";


}
于 2013-05-11T23:54:34.303 回答
1

使用计费 API

implementation 'com.android.billingclient:billing:1.1' 

使用它来获取 SKU 详细信息

public void getPrices(){


        List<String> skuList = new ArrayList<> ();
        skuList.add("id_one"); //These are the product ids in your google console
        skuList.add("id_two");
        skuList.add("id_three");
        SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
        params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
        mBillingClient.querySkuDetailsAsync(params.build(),
                new SkuDetailsResponseListener() {
                    @Override
                    public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {

                        for (SkuDetails details:
                             skuDetailsList) {

                           String item = details.getSku();
                           String price = details.getPrice();
                           String description = details.getDescription();
                           String currencyCode = details.getPriceCurrencyCode();
                           String title = details.getTitle();

                            Toast.makeText(InAppBillingActivity.this, "Finished", Toast.LENGTH_SHORT).show();

                           Log.d("hererereeer- item     ", item);
                           Log.d("hererereeer- price     ", price);
                           Log.d("hererereeer- descr     ", description);
                           Log.d("hererereeer- code     ", currencyCode);
                           Log.d("hererereeer- title     ", title);

                        }

                    }
                });
    }
于 2020-01-30T04:37:12.993 回答