【发布时间】:2015-09-08 21:43:35
【问题描述】:
Android 开发新手,我遇到了这个非常烦人且熟悉的问题。
短版:如何在不购买的情况下在多个物理设备上测试我的应用?具体来说,我正在尝试在运行 4.1 和 4.2 的设备上进行测试。我不能使用模拟器,因为这涉及应用内计费。
加长版:
今天,我收到了一个用户的崩溃报告(android 版本 = 4.2,在设备上 = A1-811 (mango))。
问题是:IAB helper is not set up. Can't perform operation: queryInventory。我在 4.3、5.0、5.1 上测试了应用,都很好。
我知道实际崩溃的发生是因为我在 mhelper.startSetup() 中检查 (!result.isSuccess()) 时没有退出。
我的问题是,如何解决 IabHelper 未设置的根本问题?!我无法访问 Android 4.2 设备...
我的代码:
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener () {
public void onIabPurchaseFinished (IabResult result, Purchase purchase) {
if (mHelper == null)
return;
if (result.isFailure ()) {
Log.d (PURCHASE_TAG, "Google's response is a failure.");
} else {
Log.d (PURCHASE_TAG, "Response is successful. purchase.getSKU = " + purchase.getSku ());
premium = true;
}
}
};
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener () {
public void onQueryInventoryFinished (IabResult result, Inventory inventory) {
Log.d (PURCHASE_TAG, "Processing Google's response.");
// Check if user has existing purchase.
if (result.isFailure ()) {
Log.d (PURCHASE_TAG, "Google's response is a failure. Response = ");
} else {
Log.d (PURCHASE_TAG, "Google's response is success! getPurchase() = " + inventory.getPurchase (THE_SKU));
if (inventory.getPurchase (THE_SKU) == null) {
premium = false;
} else {
premium = inventory.getPurchase (THE_SKU).getSku ().equals (THE_SKU);
}
}
// Show price if user is not premium, thank you note if the user is premium
if (premium == true) {
Log.d (PURCHASE_TAG, "3 premium = " + premium);
priceView.setText ("Thank you for the purchase!");
} else {
if (inventory != null) {
String pro_price = inventory.getSkuDetails (THE_SKU).getPrice ();
priceView.setText ("" + pro_price);
}
}
}
};
private void startPurchaseQuery () {
String base64EncodedPublicKey = "the key is generated ...";
mHelper = new IabHelper (this, base64EncodedPublicKey);
Log.d (PURCHASE_TAG, "Purchase query started.");
mHelper.startSetup (new IabHelper.OnIabSetupFinishedListener () {
public void onIabSetupFinished (IabResult result) {
Log.d (PURCHASE_TAG, "IabHelper Setup successful. Querying inventory.");
if (!result.isSuccess ()) {
Log.d (PURCHASE_TAG, "Error with IabHelper setup.");
return;
}
if (mHelper == null) return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
// Build the SKU list
List<String> additionalSkuList;
additionalSkuList = new ArrayList<String> ();
additionalSkuList.add (THE_SKU);
// Make the query
mHelper.queryInventoryAsync (true, additionalSkuList, mQueryFinishedListener);
Log.d (PURCHASE_TAG, "Query finished. Premium status = " + premium);
}
});
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.d (PURCHASE_TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult (requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult (requestCode, resultCode, data);
} else {
Log.d (PURCHASE_TAG, "onActivityResult handled by IABUtil.");
}
}
@Override
public void onDestroy () {
super.onDestroy ();
if (mHelper != null) mHelper.dispose ();
mHelper = null;
}
【问题讨论】:
-
不要尝试这样做。捕捉崩溃并确保在没有设置的情况下不会调用它。用户很有可能拥有您无论如何都无法重现的高度特定的设置。
-
如果我发现了它,我还能知道有多少用户因为这个问题而无法购买吗?我假设按照您的建议我不会收到崩溃报告。
-
问题是,你并没有真正失去任何东西。有这个问题的人无论如何都无法购买任何东西。他们只是不会因崩溃而烦恼。
-
听起来不错,我还将使用 Analytics 来了解有多少用户看到了这个问题。如果这个数字很大,我将发布该应用的 PRO 版本。
-
我认为这是一个好方法。您基本上会使用崩溃来衡量有多少人遇到问题,这不太理想。让我把它构造成答案,这样它就不会在“未回答”队列中。
标签: android in-app-billing android-billing