2012-08-23 47 views
2

我正在從Android提供的Dungeons示例中工作,感覺就像我幾乎正確地得到它,但我無法弄清楚如何檢測人員是否按下後退按鈕並退出購買。這是我到目前爲止: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),但不是個別條目標識ID爲「3」或「4」

編輯:

,這是我在活動開始申報產品ID:

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

謝謝!!

回答

3

根據the billing integration guide

時所要求的交易改變狀態(例如,購買成功收取信用卡 卡或用戶取消購買),在谷歌播放應用程序發送IN_APP_NOTIFY廣播意圖。 此消息包含通知ID,您可以使用該ID來檢索請求的 REQUEST_PURCHASE請求的事務處理詳細信息。

,並且打算com.android.vending.billing.PURCHASE_STATE_CHANGED包含purchaseState

訂單的購買狀態。可能的值爲0(已購買),1(已取消),2(已退款)或3 (已過期,僅限訂閱購買)。

source(表4)


編輯: 在地牢樣品,有延伸PurchaseObserver(見Dungeons活性),沒有工作,尤其參見(onPurchaseStateChange

一個內部類

編輯2:一些僞代碼

在活動(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 
    } 
} 
+0

是的我一直在盯着那個類和方法一段時間,但不知道如何編輯它。我真的希望有人分享他們如何做的代碼片段。在我的情況下,我需要將代碼帶到下一頁。另外 - 在按鈕點擊處理程序 - 是不是我應該去意向去購買頁面的地方? – Genadinik

+0

我添加了一些帶有一些註釋的僞代碼。這是很好的記錄和示例代碼提供了所需的一切(我的意思是工具)做到這一點,所以我不明白你的困難在哪裏,你可以告訴我們更多的信息(你在哪裏卡住,你不瞭解什麼...... 。)? – 2012-08-25 14:09:25

+0

我明白了,取決於itemId,我會根據他們購買的物品進入下一個屏幕。但只有一個問題,我怎麼去那個屏幕?通常我會這樣做:Intent myIntent = new Intent(ExtraHelpActivity.this,PsychologyActivity.class); \t \t \t \t ExtraHelpActivity.this.startActivity(myIntent); ....但現在我不能這樣做,因爲觀察員不是一個活動......對吧?從觀察者那裏得到一個意圖的正確方法是什麼?謝謝!! – Genadinik

1

的問題是,該呼叫mBillingService.requestPurchase不同步。因此,如果購買成功或不成功,您無法檢查。

我建議您使用庫AndroidBillingLibrary。有了它,您可以輕鬆獲得所有Google Play的活動。爲此,您可以使用助手AbstractBillingActivityAbstractBillingFragment中的onRequestPurchaseResponse()onPurchaseStateChanged()

相關問題