我正在尝试实施应用内付款'在我的应用程序中
我修改了示例TrivialDrive
应用,一切都运行得很好。现在我试图出售多件物品,但它没有像我预期的那样工作。我对代码进行了一些修改,但显然我做错了。
第一次购买完全没问题。然后在第二次收费后,应用程序崩溃了。在第三次购买时,它收取费用,但它不会在游戏中添加项目。它表现得很奇怪......
我在原始应用中做了一些更改:
我创建了多个项目,将其放入ArrayList:
static final String GOLD_3 = "gold_3";
static final String GOLD_5 = "gold_5";
//and so on
static final ArrayList<String> goods = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_screen_layout);
goods.add(GOLD_3);
goods.add(GOLD_5);
//and so on
我为不同的项目创建了多个按钮,并为他们创建了onClickListeners
:
@Override
public void onClick(View v) {
Log.d(TAG, "Buy gas button clicked.");
setWaitScreen(true);
Log.d(TAG, "Launching purchase flow for gas.");
String payload = "";
mHelper.launchPurchaseFlow(BuyScreen.this, GOLD_3, RC_REQUEST, mPurchaseFinishedListener, payload);
}
仅在不同的项目上有所不同.. gold_5
,gold_10
等。
我稍微改变了QueryInvertoryFinishedListener
,我在周期中控制是否所有购买的物品都消耗了:
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (mHelper == null)
return;
if (result.isFailure()) {
complain("Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
for (String good : goods) {
Purchase goldPurchase = inventory.getPurchase(good);
if (goldPurchase != null && verifyDeveloperPayload(goldPurchase)) {
Log.d(TAG, "We have gold. Consuming it.");
mHelper.consumeAsync(inventory.getPurchase(good), mConsumeFinishedListener);
return;
}
}
updateUi();
setWaitScreen(false);
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
再次在OnIabPurchaseFinishedListener
中添加了周期:
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
if (mHelper == null)
return;
if (result.isFailure()) {
complain("Error purchasing: " + result);
setWaitScreen(false);
return;
}
if (!verifyDeveloperPayload(purchase)) {
complain("Error purchasing. Authenticity verification failed.");
setWaitScreen(false);
return;
}
Log.d(TAG, "Purchase successful.");
for (String good : goods) {
if (purchase.getSku().equals(good)) {
Log.d(TAG, "Purchase is gas. Starting gas consumption.");
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
}
}
};
再次添加了用户购买项目类型的周期和整理:
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);
// if we were disposed of in the meantime, quit.
if (mHelper == null)
return;
if (result.isSuccess()) {
Log.d(TAG, "Consumption successful. Provisioning.");
String numberOfGold = purchase.getSku();
if (numberOfGold.equals("gold_3")) {
GameData.totalGoldCoins = +3;
} else if (numberOfGold.equals("gold_5")) {
GameData.totalGoldCoins = +5;
} else if (numberOfGold.equals("gold_10")) {
GameData.totalGoldCoins = +10;
} else if (numberOfGold.equals("gold_15")) {
GameData.totalGoldCoins = +15;
} else if (numberOfGold.equals("gold_20")) {
GameData.totalGoldCoins = +20;
}
saveData();
} else {
complain("Error while consuming: " + result);
}
updateUi();
setWaitScreen(false);
Log.d(TAG, "End consumption flow.");
}
};
...在saveData()
我使用sharedPreferences
。所有金币都通过游戏存储在静态变量的特殊类中。
我有预感,我对这些周期做错了(试图消费消费产品或其他无意义的东西)。
任何想法,我到底做错了什么?我可以以某种方式在这个已发布的(alpha)应用程序上打开logCat吗?或者,任何方式,如何测试更快(在实际交易上)然后在最终发布/更新后几个小时?
感谢您的任何想法!