2014-11-06 171 views
0

我想直接從Google Play獲取我的應用內商品的價格,而不是硬編碼。我發現以下功能從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爲空...

我應該調用哪些函數來設置服務?

+0

您是否向清單寫入權限? – 2014-11-06 10:00:24

+0

是的,一切正常,但現在我在購買按鈕上有硬編碼的價格... 點擊按鈕顯然設置了服務(從launchPurchaseFlow()?),但我想在購買前獲取價格... – EirikO 2014-11-06 13:27:38

+0

以下是它的工作方式:您在onCreate和listener中調用mIabHelper.startSetup,就像您在查詢庫存時一樣,只有在mReceivedInventoryListener中有庫存後,才能像inventory.getSkuDetails()。getPrice()和更新UI – 2014-11-06 13:39:32

回答

0

我解決了這個在我exsisting代碼做一個簡單的變化.. 我所做的是改變

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); 

而在聽者,我加

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());