Android 检查之前是否购买过内购
Android check if in app purchase was bought before
我想在我的 android 应用中进行应用内购买。我为此使用 google 示例:http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample
我在我的 android 工作室项目中实现了这个。在开发者控制台中,我将应用内购买和 gmail 地址设置为测试帐户。
在我的设备(不是模拟器)上,我使用这个测试帐户登录。我启动我的应用程序并单击 "Buy Premium" 并可以完成此过程。
现在我想显示一个按钮(文本“恢复”),如果他/她之前购买了高级功能,用户可以在其中恢复他的应用内购买。
我有这个代码:
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));
}
};
如果我再次按下 "buy" 按钮并且我之前已经购买了这个,我会在日志 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 应用中进行应用内购买。我为此使用 google 示例:http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample
我在我的 android 工作室项目中实现了这个。在开发者控制台中,我将应用内购买和 gmail 地址设置为测试帐户。
在我的设备(不是模拟器)上,我使用这个测试帐户登录。我启动我的应用程序并单击 "Buy Premium" 并可以完成此过程。
现在我想显示一个按钮(文本“恢复”),如果他/她之前购买了高级功能,用户可以在其中恢复他的应用内购买。
我有这个代码:
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));
}
};
如果我再次按下 "buy" 按钮并且我之前已经购买了这个,我会在日志 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
}
}