简体   繁体   English

通过应用内购买从Google Play获取价格

[英]Fetch prices from Google Play, in-app-purchases

I would like to get the prices for my in app items directly from Google Play, not hard coded. 我想直接从Google Play获取应用内商品的价格,而不是硬编码。 I found the following function 我发现以下功能

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";
}

The problem is that I try to use this code before the Service is binded. 问题是我在绑定服务之前尝试使用此代码。 (mService is null) (mService为空)

In my onCreate() I do some UI-setup, then I call 在我的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);
            }
        }
    });

And then I try to call the mIabHelper.getPricesDev() , but it results in null pointer, due to mService is null... 然后我尝试调用mIabHelper.getPricesDev() ,但是由于mService为null,所以它会导致null指针。

What functions should I call to set up the service? 我应该调用什么功能来设置服务?

I solved this by doing a simple change in my exsisting code.. What I did was to change 我通过对现有代码进行简单的更改来解决了这个问题。我所做的就是更改

mIabHelper.queryInventoryAsync(mReceivedInventoryListener);

to

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

And in the listener, I added 在听众中,我添加了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM