我想直接从Google Play获取我的应用内商品的价格,而不是硬编码。我找到了以下功能
public String getPricesDev() throws RemoteException, JSONException{
String packageName = mContext.getPackageName();
ArrayList<String> skuList = new ArrayList<String>();
skuList.add("batforerproven.permanent.cat1"); //Is this correct? Name of products?
skuList.add("batforerproven.permanent.cat2");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle skuDetails = mService.getSkuDetails(3,packageName, ITEM_TYPE_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("batforerproven.permanent.cat2")) return price;
}
}
return "Not found";
}
问题是我在捆绑服务之前尝试使用此代码。 (mService为null)
在我的onCreate()中,我进行了一些UI设置,然后调用
mIabHelper = new IabHelper(this, base64EncodedPublicKey);
mIabHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Toast.makeText(BuyExtras.this, "Feil ved kobling mot Google Play.", Toast.LENGTH_LONG).show();
} else {
mIabHelper.queryInventoryAsync(mReceivedInventoryListener);
}
}
});
然后我尝试调用mIabHelper.getPricesDev()
,但由于mService为null,它会导致空指针...
我应该拨打哪些功能来设置服务?
答案 0 :(得分:0)
我通过对现有代码进行简单更改来解决这个问题。 我做的是改变
mIabHelper.queryInventoryAsync(mReceivedInventoryListener);
到
ArrayList<String> additionalSkuList = new ArrayList<String>();
additionalSkuList.add(ITEM_CAT_1);
additionalSkuList.add(ITEM_CAT_2);
additionalSkuList.add(ITEM_CAT_3);
additionalSkuList.add(ITEM_PREMIUM);
mIabHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
在听众中,IS添加了
cat1Desc.setText(inventory.getSkuDetails(ITEM_CAT_1).getDescription());
cat2Desc.setText(inventory.getSkuDetails(ITEM_CAT_2).getDescription());
cat3Desc.setText(inventory.getSkuDetails(ITEM_CAT_3).getDescription());
PremiumDesc.setText(inventory.getSkuDetails(ITEM_PREMIUM).getDescription());
buyCat1Btn.setText(inventory.getSkuDetails(ITEM_CAT_1).getPrice());
buyCat2Btn.setText(inventory.getSkuDetails(ITEM_CAT_2).getPrice());
buyCat3Btn.setText(inventory.getSkuDetails(ITEM_CAT_3).getPrice());
buyPremiumBtn.setText(inventory.getSkuDetails(ITEM_PREMIUM).getPrice());