InApp Billing:java.lang.SecurityException需要READ_PHONE_STATE

时间:2016-06-30 19:12:29

标签: android in-app-purchase in-app-billing securityexception

我在谷歌商店的应用崩溃报告中遇到了很多例外。 有人能解释一下可能导致这种情况的原因吗?我使用的是android:targetSdkVersion = 19

java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10131 nor current process has android.permission.READ_PHONE_STATE.
    at android.os.Parcel.readException(Parcel.java:1546)
    at android.os.Parcel.readException(Parcel.java:1499)
    at com.android.vending.billing.IInAppBillingService$Stub$Proxy.getSkuDetails(IInAppBillingService.java:251)
    at com.inapp.util.IabHelper.querySkuDetails(IabHelper.java:920)
    at com.inapp.util.IabHelper.queryInventory(IabHelper.java:550)
    at com.inapp.util.IabHelper.queryInventory(IabHelper.java:522)
    at com.inapp.util.IabHelper$2.run(IabHelper.java:617)
    at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:3)

虽然Google已经确认已经发布了具有此修复程序的更新,但以下尝试catch阻止至少可以防止崩溃

int querySkuDetails(String itemType, Inventory inv, List<String> moreSkus)
                                throws RemoteException, JSONException {
        logDebug("Querying SKU details.");
        ArrayList<String> skuList = new ArrayList<String>();
        skuList.addAll(inv.getAllOwnedSkus(itemType));
        if (moreSkus != null) {
            for (String sku : moreSkus) {
                if (!skuList.contains(sku)) {
                    skuList.add(sku);
                }
            }
        }

        if (skuList.size() == 0) {
            logDebug("queryPrices: nothing to do because there are no SKUs.");
            return BILLING_RESPONSE_RESULT_OK;
        }

        // NullPointer crash reported through PlayStore forums
        if (mService == null) {
            return IABHELPER_SERVICE_UNAVAILABLE;
        }

        Bundle querySkus = new Bundle();
        querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, skuList);

        try {
                    Bundle skuDetails = mService.getSkuDetails(3, mContext.getPackageName(), itemType, querySkus);
                    if (!skuDetails.containsKey(RESPONSE_GET_SKU_DETAILS_LIST)) {
                        int response = getResponseCodeFromBundle(skuDetails);
                        if (response != BILLING_RESPONSE_RESULT_OK) {
                            logDebug("getSkuDetails() failed: " + getResponseDesc(response));
                            return response;
                        }
                        else {
                            logError("getSkuDetails() returned a bundle with neither an error nor a detail list.");
                            return IABHELPER_BAD_RESPONSE;
                        }
                    }

                    ArrayList<String> responseList = skuDetails.getStringArrayList(RESPONSE_GET_SKU_DETAILS_LIST);

                    for (String thisResponse : responseList) {
                        SkuDetails d = new SkuDetails(itemType, thisResponse);
                        logDebug("Got sku details: " + d);
                        inv.addSkuDetails(d);
                    }
                    return BILLING_RESPONSE_RESULT_OK;
                }
                // Security Exception due to missing permissions reported through PlayStore forums
                catch (SecurityException e)
                {
                    return IABHELPER_SERVICE_UNAVAILABLE;
                }
}
  

请注意,方法int querySkuDetails(String itemType,Inventory inv,List moreSkus)中唯一的更改是Security Exception的try catch块。休息一切都一样。

答案 1 :(得分:-7)

在AndroidManifest.xml中添加适当的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />