我正在为一个应用程序进行谷歌应用内购买。我创建了一个 id 为“app.test.item”的托管产品项目。我遵循了谷歌文档中指定的所有其他步骤。第一次购买应用程序运行良好。但是当我尝试卸载应用程序并再次安装时。该应用程序运行恢复事务请求。onRestoreTransactionsResponse() 方法被调用,我根据从市场返回的 RESULT_OK 使购买状态为真。我的疑问是应用程序如何知道只有名为“app.test.item”的项目被购买并因此被恢复。我还有其他一些项目,例如 "app.test.second.item" 。应用内计费如何看出两者之间的差异。我使用了 Google 提供的应用内结算代码。刚刚提到了一些共享偏好来存储购买状态。我在这里做错什么了吗。请指导我。
我也想在调试模式下测试这个应用程序,以便我可以追踪工作流程。但是应用程序应该使用发布版本进行签名。有什么方法可以在调试版本中完全运行这个应用程序(测试恢复事务)。我了解使用保留 productId 作为“android.test.purchased”进行一般购买的一般工作流程。但我需要测试 restoreTransactions()。任何帮助表示赞赏。
我在这里发布 PurchaseObserver 示例代码。
谢谢,
private class DungeonsPurchaseObserver extends PurchaseObserver {
public DungeonsPurchaseObserver(Handler handler) {
super(Dungeons.this, handler);
}
@Override
public void onBillingSupported(boolean supported) {
if (Consts.DEBUG) {
Log.i(TAG, "supported: " + supported);
}
if (supported) {
restoreDatabase();
} else {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
if (developerPayload == null) {
logProductActivity(itemId, purchaseState.toString());
} else {
logProductActivity(itemId, purchaseState + "\n\t" + developerPayload);
}
if (purchaseState == PurchaseState.PURCHASED) {
mOwnedItems.add(itemId);
logProductActivity("APPLICATION", ": STATE PURCHASED");
SharedPreferences pref = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PURCHASE_STATUS, "ITEM PURCHASED");
editor.commit();
}
if(purchaseState == PurchaseState.CANCELED)
{
logProductActivity("APPLICATION", ": STATE CANCELLED");
SharedPreferences pref = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PURCHASE_STATUS, "ITEM CANCELLED");
editor.commit();
}
if(purchaseState == PurchaseState.REFUNDED)
{
logProductActivity("APPLICATION", ": STATE REFUNDED");
SharedPreferences pref = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PURCHASE_STATUS, "ITEM REFUNDED");
editor.commit();
}
}
@Override
public void onRequestPurchaseResponse(RequestPurchase request,
ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
logProductActivity(request.mProductId, "sending purchase request");
} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
logProductActivity(request.mProductId, "dismissed purchase dialog");
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
logProductActivity(request.mProductId, "request purchase returned " + responseCode);
}
}
@Override
public void onRestoreTransactionsResponse(RestoreTransactions request,
ResponseCode responseCode) {
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.d(TAG, "completed RestoreTransactions request");
}
// Update the shared preferences so that we don't perform
// a RestoreTransactions again.
SharedPreferences prefs = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(DB_INITIALIZED, true);
edit.putString(PURCHASE_STATUS, "ITEM PURCHASE RESTORED");
edit.commit();
logProductActivity("onRestoreTransactionsResponse() method in PurchaseObserver", "ITEM PURCHASE RESTORED");
} else {
if (Consts.DEBUG) {
Log.d(TAG, "RestoreTransactions error: " + responseCode);
}
}
logProductActivity("onRestoreTransactionsResponse() method in PurchaseObserver", responseCode.toString());
}
}