2

我正在从 Android 提供的地下城示例中工作,感觉我几乎得到了正确的结果,但我不知道如何检测该人是否按下了后退按钮并退出了购买。这是我到目前为止所拥有的:

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)但不是个人如果里面的案例,即使 itemId id 是“3”或“4”

编辑:

这就是我在活动开始时声明产品 ID 的方式:

String issueProductIdWebMarketing = "1";    
    String issueProductIdDonate = "2";  
    String issueProductIdPsych = "3";
    String issueProductIdNumberofBusinesses = "4";

谢谢!!

4

2 回答 2

3

根据计费集成指南

当请求的交易改变状态时(例如,购买成功通过信用卡支付或用户取消购买),Google Play 应用程序发送一个 IN_APP_NOTIFY 广播意图。此消息包含一个通知 ID,您可以使用它来检索 REQUEST_PURCHASE 请求的交易详细信息。

意图com.android.vending.billing.PURCHASE_STATE_CHANGED包含purchaseState

订单的购买状态。可能的值为 0(已购买)、1(已取消)、2(已退款)或 3(已过期,仅用于订阅购买)。

来源(表 4)


编辑:在地牢示例中,有一个内部类扩展PurchaseObserver(参见Dungeons活动)来完成这项工作,尤其是(onPurchaseStateChange


EDIT2:一些伪代码

在活动中(Dungeonssdk 示例中的类):

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
    }
}
于 2012-08-25T13:30:11.283 回答
1

问题是调用mBillingService.requestPurchase不是同步的。因此,您无法在购买成功后立即检查。

我建议您使用库AndroidBillingLibrary。有了它,您可以轻松获取 Google Play 的所有活动。为此,您可以从助手中使用onRequestPurchaseResponse()and或.onPurchaseStateChanged()AbstractBillingActivityAbstractBillingFragment

于 2012-08-26T12:00:41.467 回答