- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在处理应用内购买示例。我的应用程序有 2 个按钮,第一个按钮默认禁用,单击第二个按钮将购买并启用第一个按钮。购买按钮可以正常使用,但购买后,我检查了库存,但它总是返回 null,这意味着我还没有购买。
这是源代码:
设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buyButton = (Button) findViewById(R.id.buyButton);
clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setEnabled(false);
String base64EncodedPublicKey = "key";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " + result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
// Query to detect user was buy this item or not
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
});
buyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (clickButton.isEnabled()) {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
} else {
buyClick(v);
}
}
});
}
购买商品点击:
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "");
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
// Handle error
return;
} else if (purchase.getSku().equals(ITEM_SKU)) {
// consumeItem();
// buyButton.setEnabled(false);
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
}
};
听众:
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
Toast.makeText(getApplicationContext(),
"Query Inventory Error!", Toast.LENGTH_SHORT).show();
// Handle failure
} else {
Toast.makeText(getApplicationContext(),
"Query Inventory Success!", Toast.LENGTH_SHORT).show();
// mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
// mConsumeFinishedListener);
// if (inventory.hasPurchase(ITEM_SKU)) {
// clickButton.setEnabled(true);
// }
Purchase item = inventory.getPurchase(ITEM_SKU);
if (item != null) {
clickButton.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(),
"This item was not buy yet!", Toast.LENGTH_SHORT)
.show();
}
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
Toast.makeText(getApplicationContext(), "Consume done!",
Toast.LENGTH_SHORT).show();
clickButton.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "Consume Error!",
Toast.LENGTH_SHORT).show();
// handle error
}
}
};
当第一次点击购买按钮时,它会显示支付对话框,支付成功后,clickButton 被启用。
但是当我第二次点击 buyButton 时,它会转到库存,但 inventory.getPurchase(ITEM_SKU) 总是返回 null。
有什么想法吗?
谢谢!
最佳答案
只需从 OnIabPurchaseFinishedListener "mHelper.consumeAsync(purchase, mConsumeFinishedListener);"中评论或删除您的代码
If you prefer Android default Trivial demo then go for the premium product purchase for the single time product purchase.
OnIabPurchaseFinishedListener()
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: "
+ purchase);
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.");
if (purchase.getSku().equals(SKU_PREMIUM)) {
// bought the premium upgrade!
Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
alert("Thank you for upgrading to premium!");
}
}
};
Explaination why it gives null data :
Keep it mind that Google will store data from their side only when you are trying to purchase one time purchase product. But Whenever you purchased consumable item google play store will not be managed it's product purchased detail and other things in the Google play console. That's why we have to call consumeAsync() method. when we purchased item, Google play store keep record item has been purchased for the one time and allow you to purchased second time.
更多信息: android: Inapp billing: Error response: 7:Item Already Owned
但是,在这里您尝试将产品作为单次购买进行购买,因此您一定不需要在 onIabPurchasedFinishListener 中调用 consumeAsync() 方法。
希望能解决您的问题。
关于android - inventory.getPurchase() 总是返回 null,尽管已经购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512417/
一段时间以来,我一直在做这个反复出现的噩梦(阅读 - 我的应用程序中的错误)。出于某种原因,某个计时器在我停止后继续发送“Elapsed”事件,即使 在事件本身 计时器“承认”已被禁用!检查一下: /
为了找到 2 个 git 分支的共同祖先,需要做的是: git merge-base branch another_branch 好的。但是……如果两个分支都已经 merge 了怎么办?当我在这种情况
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我想要一个相机 View ,可以将图像捕获到本地文件或让用户从本地照片库中选择图像。我想也许有人为此编写了很好的库/代码。也许我可以利用它。已经有好的了吗?谢谢。我只是避免重新发明轮子:) 最佳答案
我从 master 分支创建了一个功能分支。之后有来自功能分支的提交 [F1]。 [F1] -- Feature Branch / [M1]-[M2
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
介绍和搜索 所以我认为我犯了一个严重的错误,我很担心。我已经分析了独立负责人的论坛,我已经接近找到答案,但场景太具体,不适用于我所在的位置。如果您找到可以回答我的问题的特定主题,请链接我。 例如:Ho
我有一个类似于下图的提交图。标记为 * 的提交表示大量提交。 A* | B--------- | | C* D* master 和 cor
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
我们最近接手了一个 .NET 项目,在查看 db 后,我们在某些列中有以下内容: 1)某些列具有诸如" & etc etc 2) 有些有 标签和其他非 html 编码的标签 这些数据
你好,当我导航到应用程序中的另一个页面时出现此错误 我不知道为什么这个错误出现 #0 _AsyncCompleter.complete (dart:async/future_impl.da
我使用以下 C 算法计算数据的 CRC32: #define CRC32_POLYNOM_REVERSED 0xEDB88320 uint32 calcCrc32(uint8* buffer, u
我试图在我的一个测试中断言模型中的字段没有改变。我知道从哲学上这是不正确的,但由于我控制了我需要知道的所有变量,所以我只想检查我的数据库条目是否没有改变。 我愿意接受一个解决方案,该解决方案可以将其转
我是 GitHub 的新手。并通过 Eclipse 使用它我们是两个人在开发一个应用程序。当我在 Git shell 中检查 git status 时,我得到以下状态。 On branch maste
简单代码: std::ifstream file("file.txt"); std::string line; while(getline(file,line)) ; //exhaust file
是的,我又找不到这个 Gradle DSL 方法:'compile()' 问题。 我检查了我有: buildscript { repositories { jcenter()
HTML: articles CSS: #main_menu { float: left; padding-top: 10px; vertical-align: m
我是一名优秀的程序员,十分优秀!