应用内购买 getPrice() Android

12

我已经成功地将应用内计费集成到我的应用程序中,一切都很顺利。现在我正在尝试检索商品的价格(在开发者控制台中设置),以便在不硬编码值的情况下在我的应用程序中反映这些价格。

这段代码明显仅收集通过库存已购买的物品的价格,这不是我要找的内容:

SkuDetails gasDetails = inventory.getSkuDetails(SKU_FULL);      

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

我已经查看了文档来查询可购买的商品,但是很难理解。我原以为 Helper 类应该实现某种获取价格的方法。

我的问题是:有人能指点一下我正确的方向吗?

4个回答

9
如果您正在使用谷歌提供的“TrivialDrive”示例中提出的实现方式,您可以在查询库存的方法中将参数“details”和“moreSkus”设置为true,从而检索所有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 {

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


}

2
使用计费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);

                        }

                    }
                });
    }

0
private fun getPrices() {
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                val queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
                    .setProductList(
                        ImmutableList.of(
                            QueryProductDetailsParams.Product.newBuilder()
                                .setProductId("product_id")
                                .setProductType(BillingClient.ProductType.INAPP)
                                .build()
                        )
                    )
                    .build()
                billingClient.queryProductDetailsAsync(
                    queryProductDetailsParams
                ) { res, list ->
                    val priceAmountMicros: Long =
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.priceAmountMicros!!

                    Log.d(
                        TAG,
                        (priceAmountMicros.toDouble() / 1000000).toString()
                    )
                    Log.d(
                        TAG,
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.priceCurrencyCode.toString()
                    )
                    Log.d(
                        TAG,
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.formattedPrice.toString()
                    )
                }
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接