我想在我的Android应用程序中进行应用内购买.我使用google示例: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中收到此消息:
- >:购买完成:IabResult:无法购买商品(回复:7:商品已经拥有),购买:null - >:购买错误:IabResult:无法购买商品(回复:7:商品已经拥有)
我的问题是,如何检查应用内购买之前是否购买过?
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 } }