[英]In App Billing getPrice() Android
我已经成功地在我的应用程序中实施了应用程序计费,一切正常。 我现在正在尝试检索项目的价格(在开发人员控制台中设置),以便我可以在我的应用程序中反映这些价格,而无需硬编码值。
这段代码很明显只收集已经通过库存购买的物品的价格,这不是我想要的:
SkuDetails gasDetails = inventory.getSkuDetails(SKU_FULL);
if (gasDetails != null){
alert("Gas is " + gasDetails.getPrice());}
我查看了查询可购买物品的文档,但很难理解它。 我认为 Helper 类会实现某种获取价格的方法。
所以,我的问题是:谁能指出我正确的方向?
如果您使用的是 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 {
好的,我找到了解决方案。 我已经破译了开发人员文档,并且其中似乎存在错误。
这是我在 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";
}
使用计费 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);
}
}
});
}
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()
)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.