在v4版本中,我曾使用SkuDetails.price来获取产品的价格,但在v5中的新ProductDetails中不再可用。
如何在这个新版本中获取产品的价格?
如何在这个新版本中获取产品的价格?
getSubscriptionOfferDetails
时,它会返回可购买的订阅产品优惠。然后,您可以调用getPricingPhases()
来获取定价阶段列表。每个定价阶段对象都有一个getFormattedPrice()
调用,以获取优惠定价阶段的价格(https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.PricingPhase)。OnetimePurchaseOfferDetails
对象,可以调用 getFormattedPrice()
方法获取格式化后的价格信息。具体信息请查阅:https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.OneTimePurchaseOfferDetails。 - misobean你需要检查可用产品
fun getAvailableProducts() {
Timber.d("!!! Getting available products to buy ...")
val queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
.setProductList(
listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(SKU_SUBSCRIBE_MONTHLY)
.setProductType(BillingClient.ProductType.SUBS)
.build(),
QueryProductDetailsParams.Product.newBuilder()
.setProductId(SKU_SUBSCRIBE_YEARLY)
.setProductType(BillingClient.ProductType.SUBS)
.build()
))
.build()
billingClient.queryProductDetailsAsync(queryProductDetailsParams) {
billingResult,
productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
availableProducts.tryEmit(productDetailsList)
getPrices(productDetailsList)
} else {
Timber.d("!!!Error getting available Products to buy: ${billingResult.responseCode} ${billingResult.debugMessage}")
}
}
}
然后
private fun getPrices(productDetailsList: MutableList<ProductDetails>) {
productDetailsList.forEach{
when (it.productId) {
SKU_SUBSCRIBE_MONTHLY -> {
currency.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.priceCurrencyCode.toString())
monthlyPrice.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.formattedPrice.toString())
Timber.d("!!!! $it.")
}
SKU_SUBSCRIBE_YEARLY -> {
// currency.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.priceCurrencyCode.toString())
yearlyPrice.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.formattedPrice.toString())
Timber.d("!!!! $it.")
}
}
}
}
QueryProductDetailsParams queryProductDetailsParams
= QueryProductDetailsParams.newBuilder().setProductList(
ImmutableList.of(QueryProductDetailsParams.Product.newBuilder()
.setProductId("your_product_id")
.setProductType(BillingClient.ProductType.INAPP).build()))
.build();
billingClient.queryProductDetailsAsync(
queryProductDetailsParams,
new ProductDetailsResponseListener() {
@Override
public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<ProductDetails> list) {
if(!list.isEmpty()){
productDetails = list.get(0);
itemdesc.setText(productDetails.getName());
itemprice.setText(productDetails.getOneTimePurchaseOfferDetails().getFormattedPrice());
itemprice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makePurchase();
}
});
}else {
Log.i("playsresponse", "no response from google play");
}
}
}
);