我已经成功将应用结算应用到我的应用中,一切正常。我现在正在尝试检索商品的价格(在开发者控制台中设置),以便可以在我的应用程序中反映这些价格而无需对值进行硬编码。
显然,此代码仅收集已经通过库存购买的商品的价格,这不是我想要的:
SkuDetails gasDetails = inventory.getSkuDetails(SKU_FULL);
if (gasDetails != null){
alert("Gas is " + gasDetails.getPrice());}
我看过docs个可购买的查询物品,但难以理解。我以为Helper类会实现某种get price方法。
所以,我的问题:有人能指出我正确的方向吗?
[如果您使用的是Google在“ TrivialDrive”示例中建议的实现,则可以通过将true传递给方法中的参数“ details”和“ moreSkus”来检索所有skus的信息(即使未购买)。查询库存
/** * 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);
}
}
});
}