在实际显示应用内购买的Android市场前端之前,如何检索应用内结算项目的价格?目前看起来用户只能在购买对话框中找到应用内商品的价格,我希望避免将所有支持货币的价格存储在应用程序中。
答案 0 :(得分:17)
现在可以使用Billing API v3。您可以使用getSkuDetails()
方法获取信息。示例是here。
ArrayList skuList = new ArrayList();
skuList.add("premiumUpgrade");
skuList.add("gas");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);
Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), “inapp”, querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList 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.equals(“premiumUpgrade”)) {
mPremiumUpgradePrice = price;
} else if (sku.equals(“gas”)) {
mGasPrice = price;
}
}
}
答案 1 :(得分:17)
如果您使用的是Trivial Drive示例并包含IabHelper类,则需要将skus列表传递给queryInventoryAsync。
String[] moreSkus = {"SKU_ITEMONE", "SKU_ITEMTWO"};
mHelper.queryInventoryAsync(true, Arrays.asList(moreSkus), mGotInventoryListener);
答案 2 :(得分:2)
String price = inventory.getSkuDetails("sku_of_your_product").getPrice();
答案 3 :(得分:0)
如果您不遵循Google琐事示例,则可以使用此代码获取价格。
private void getItemsPrice(){
List<String> skuList = new ArrayList<>();
skuList.add("example1");
skuList.add("example2");
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) {
if (responseCode == BillingClient.BillingResponse.OK
&& skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
Log.d("item price",price);
if ("example1".equals(sku)) {
tvPlan1.setText(price);
}
else if ("example2".equals(sku)){
tvPlan2.setText(price);
}
}
}
}
});
}