Android 计费 - 如何设置购买按钮并正确读取用户是否取消或购买?

标签 android android-billing

我正在使用 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/

相关文章:

android - 滚动时带有 ViewHolder 和部分的 ListView 失败

Android 布局编辑器设备不适合每个不同的布局

java - 使用 SQLite 进行简单的数学计算 - Android

android - Android 应用中的 PayPal 系统错误 550006

android - 具有多个帐户的应用内购买

android - 应用内结算,无法让 RESULT_DEVELOPER_ERROR 消失

Android 计费测试 - 如何与测试用户进行测试?

android - 处理 appwidget 的多个实例

android - 是否可以在我的免费 Android 应用中添加应用内付费功能?

java - 将 switch 语句与 imageview 可见性连接起来