我想在我的Android应用中进行应用内购买。我使用谷歌样本:http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample
我在我的android studio项目中实现了这个。在开发者控制台中,我将应用内购买和gmail地址设置为测试帐户。
在我的设备上(不是模拟器),我使用此测试帐户登录。我开始使用我的应用程序并点击"购买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));
}
};
如果我按下"购买"按钮再次,我以前买过这个,我在log cat中收到这条消息:
- >:购买完成:IabResult:无法购买商品(回复:7:商品已经拥有),购买:null - &gt ;:购买错误:IabResult:无法购买商品(回复:7:商品已经拥有)
我的问题是,如何检查应用内购买是否已经购买?
答案 0 :(得分:11)
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
}
}