gpt4 book ai didi

Android检查之前是否购买了应用内购买

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:42 26 4
gpt4 key购买 nike

我想在我的 Android 应用中进行应用内购买。我为此使用了谷歌样本:http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample

我在我的 android studio 项目中实现了这个。在开发人员控制台中,我将应用内购买和 gmail 地址设置为测试帐户。

在我的设备(不是模拟器)上,我使用这个测试帐户登录。我启动我的应用程序并点击“购买高级版”,然后可以完成此过程。

现在我想显示一个按钮(文本“恢复”),如果他/她之前购买了高级功能,用户可以在其中恢复他的应用内购买。

我有这个代码:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.e("-->", "Purchase finished: " + result);



if (mHelper == null) return;

if (result.isFailure()) {
Log.e("-->","Error purchasing: " + result);
return;
}
if (!verifyDeveloperPayload(purchase)) {
Log.e("-->","Error purchasing. Authenticity verification failed.");
return;
}



SharedPreferences prefs = this.getSharedPreferences("xxx", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Premium", true);
editor.commit();
Log.e("-->", "Premium: " + prefs.getBoolean("Premium", false));


}
};

如果我再次按下“购买”按钮并且我之前购买过这个,我会在 log cat 中收到此消息:

-->: Purchase finished: IabResult: Unable to buy item (response: 7:Item Already Owned), purchase: null -->: Error purchasing: IabResult: Unable to buy item (response: 7:Item Already Owned)

我的问题是,如何检查之前是否购买过应用内购买?

最佳答案

 mIabHelper.queryInventoryAsync(true, "your_sku", mGotInventoryListener);

// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d("PAY", "Query inventory finished.");

// Have we been disposed of in the meantime? If so, quit.
if (mIabHelper == null) return;
Purchase purchase = inventory.getPurchase("your_sku");
if (purchase != null) {
//purchased
}
}

关于Android检查之前是否购买了应用内购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com