How can the price of an in-app billing item be retrieved before actually displaying the android market frontend for the in-app purchase? Currently it looks like the user only can find out the price for an in-app item in the purchase dialog and i would like to avoid to store the prices in the application for all supported currencies.
It is possible now with Billing API v3. You can get information with getSkuDetails()
method. Example is 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;
}
}
}
If you are using the Trivial Drive example and included IabHelper class you need to pass a list of skus to queryInventoryAsync.
String[] moreSkus = {"SKU_ITEMONE", "SKU_ITEMTWO"};
mHelper.queryInventoryAsync(true, Arrays.asList(moreSkus), mGotInventoryListener);
Cícero Moura
String price = inventory.getSkuDetails("sku_of_your_product").getPrice();
If you are not following the google trivia example then you can get price by using this code.
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);
}
}
}
}
});
}
来源:https://stackoverflow.com/questions/5533676/android-in-app-billing-item-price