我已经从本教程中实现了应用内计费。要购买的项目是设置自定义背景的能力。效果很好,但是当我卸载并重新安装应用程序(或清除用户首选项)时,我无法弄清楚如何验证是否有人已经购买了应用程序内商品。
public void buySelected() {
if (backgroundColorsPurchased == true) {
this.colorChangeDialog(); //if user has already purchased, just call the dialog instead of re-buying.
//if the person has cleared their prefs, they'll have to be online to re-verify that they did indeed buy the item.
}else{
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "background.colors");
BillingHelper.setCompletedHandler(mTransactionHandler);
} else {
Log.i(TAG,"Can't purchase on this device");
}
}
}
然后我有处理程序:
public Handler mTransactionHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);
if(BillingHelper.latestPurchase.isPurchased()){
//this is where we show the stuff that the person purchased. In this case, the dialog to change the background colour.
backgroundColorsPurchased = true; //just setting this to true so that the next time somebody clicks the donate button it'll just open the dialog.
//call the change background dialog
colorChangeDialog();
}else{
//fail
Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
}
}
};
我如何能够验证该物品之前是否已购买?市场只是不断弹出一个对话窗口,上面写着“您已经购买了该商品,或者该购买仍在等待中”。当我尝试使用类似的东西时,if(BillingHelper.latestPurchase.isPurchased()){
如果它不在处理程序中,我会强制关闭。