我正在使用 Android 提供的 Dungeons 示例,感觉我几乎正确地理解了它,但我不知道如何检测该人是否按下后退按钮并退出购买。这是我到目前为止所拥有的:
Button buy = (Button)findViewById(R.id.buy);
buy.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
try
{
if (mBillingService.requestPurchase(issueProductId,
Consts.ITEM_TYPE_INAPP , null))
{
// Not sure what to do here. Has the person been sent to the buy screen yet or no?
// There are these strings I need to check for but not entirely certain how:
if(mBillingService.checkBillingSupported(Consts.ITEM_TYPE_INAPP))
{
// OK
}
else
{
// If billing is not supported, give them the item for free
Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
ExtraHelpActivity.this.startActivity(myIntent);
}
try
{
if (mBillingService.requestPurchase(issueProductIdPsych,
Consts.ITEM_TYPE_INAPP , null))
{
// HOW DO I CHECK HERE IF THE USER CANCELED OUT OF THE PURCHASE?
// EVEN ON CANCEL, THEY ARE BEING TAKEN TO THE ITEM THAT THEY NEED TO PAY FOR.
// Send them to the screen of the article.
Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
ExtraHelpActivity.this.startActivity(myIntent);
}
}
catch ( Exception e )
{
// Report exception.
}
});
我在我感到困惑的地方添加了一些内嵌注释。我不确定如何判断该人是否购买了该商品或取消了该商品。如果有人能帮助我解决这个问题,那就太好了。
现在,此代码会将用户带到购买屏幕,有些人会购买,但当人们按取消时,他们仍然会被带到他们没有购买的商品。 :)
编辑:
顺便说一句,如果用户已经购买了该商品并想再次购买,则购买请求状态不会改变,对吧?那么最终会触发什么方法呢?我测试了一次购买,并且能够购买对一篇文章的一个屏幕的访问权限,但现在无法再次访问它,因为付款屏幕一直告诉我我已经购买了该商品,并且没有带我进入下一页。
这是我的新 onStateChanged 方法的样子:
@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload)
{
if (purchaseState == PurchaseState.PURCHASED)
{
mOwnedItems.add(itemId);
if ( itemId != null && itemId.trim().equals("3") )
{
Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
ExtraHelpActivity.this.startActivity(myIntent);
}
if ( itemId != null && itemId.trim().equals("4") )
{
Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
}
}
else
if (purchaseState == PurchaseState.CANCELED)
{
// purchase canceled
}
else
if (purchaseState == PurchaseState.REFUNDED)
{
// user ask for a refund
}
else
{
if ( itemId != null && itemId.equals("3") )
{
Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
ExtraHelpActivity.this.startActivity(myIntent);
}
if ( itemId != null && itemId.equals("4") )
{
Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
ExtraHelpActivity.this.startActivity(myIntent);
}
}
}
当我测试它并购买一篇文章时,它确实到达了 if (purchaseState == PurchaseState.PURCHASED)
但不是其中的单个 if 案例,即使 itemId id 是“3”或“4”
编辑:
这就是我在 Activity 开始时声明产品 ID 的方式:
String issueProductIdWebMarketing = "1";
String issueProductIdDonate = "2";
String issueProductIdPsych = "3";
String issueProductIdNumberofBusinesses = "4";
谢谢!!
最佳答案
根据the billing integration guide :
when the requested transaction changes state (for example, the purchase is successfully charged to a credit card or the user cancels the purchase), the Google Play application sends an IN_APP_NOTIFY broadcast intent. This message contains a notification ID, which you can use to retrieve the transaction details for the REQUEST_PURCHASE request.
Intent com.android.vending.billing.PURCHASE_STATE_CHANGED
包含purchaseState
The purchase state of the order. Possible values are 0 (purchased), 1 (canceled), 2 (refunded), or 3 (expired, for subscription purchases only).
source (表4)
编辑:
在 dungeons 示例中,有一个扩展 PurchaseObserver
的内部类(请参阅 Dungeons
Activity )来完成这项工作,特别是 (onPurchaseStateChange
)
EDIT2:一些伪代码
在 Activity 中(sdk 示例中的 Dungeons
类):
Button buy = (Button)findViewById(R.id.buy);
buy.setEnabled(false);
buy.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
// show purchase dialog and call mBillingService.requestPurchase() on dialog validation
}
});
// request to see if supported
if (!mBillingService.checkBillingSupported()) {
// not supported
}
在观察者中(示例中的DungeonsPurchaseObserver
):
public void onBillingSupported(boolean supported, String type) {
if (type == null || type.equals(Consts.ITEM_TYPE_INAPP)) {
if (supported) {
// billing supported: enable buy button
} else {
// billing not supported: disable buy button
}
}
}
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (purchaseState == PurchaseState.PURCHASED) {
// item purchased
} else if (purchaseState == PurchaseState.CANCELED) {
// purchase canceled
} else if (purchaseState == PurchaseState.REFUND) {
// user ask for a refund
}
}
关于Android 计费 - 如何设置购买按钮并正确读取用户是否取消或购买?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092431/