2017-01-11 159 views
0

我該如何多次購買一件東西?這裏是我的代碼:Android應用內結算 - 如何多次購買單個應用內購買?

// [...] 
    String base64EncodedPublicKey = 
       "MIIB...; 

     mHelper = new IabHelper(this, base64EncodedPublicKey); 

     mHelper.startSetup(new 
            IabHelper.OnIabSetupFinishedListener() { 
             public void onIabSetupFinished(IabResult result) { 
              if (!result.isSuccess()) { 
               Toast.makeText(gift.this, "Setup no Success", Toast.LENGTH_SHORT).show(); 
               Log.d(TAG, "In-app Billing setup failed: " + result); 
              } else { 
               Toast.makeText(gift.this, "Setup Success", Toast.LENGTH_SHORT).show(); 
              } 
             } 
            }); 


    } 


    public void insert(View view) { 
     mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, 
       mPurchaseFinishedListener, "mypurchasetoken"); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent data) { 
     if (!mHelper.handleActivityResult(requestCode, 
       resultCode, data)) { 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
      = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, 
              Purchase purchase) 
     { 
      if (result.isFailure()) { 
       Toast.makeText(gift.this, "Error 1", Toast.LENGTH_SHORT).show(); 
      } 
      else if (purchase.getSku().equals(ITEM_SKU)) { 
       consumeItem(); 
       Toast.makeText(gift.this, "OK 1", Toast.LENGTH_SHORT).show(); 
       // mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
      } 

     } 
    }; 
    public void consumeItem() { 
     mHelper.queryInventoryAsync(mReceivedInventoryListener); 
    } 

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
      = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, 
              Inventory inventory) { 

      if (result.isFailure()) { 
       Toast.makeText(gift.this, "Error 2", Toast.LENGTH_SHORT).show(); 
      } else { 
       mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), 
         mConsumeFinishedListener); 
       Toast.makeText(gift.this, "OK 2", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }; 
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
      new IabHelper.OnConsumeFinishedListener() { 
       public void onConsumeFinished(Purchase purchase, 
               IabResult result) { 

        if (result.isSuccess()) { 
         Toast.makeText(gift.this, "Success 1", Toast.LENGTH_SHORT).show(); 
         // mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
        } else { 
         Toast.makeText(gift.this, "Error 3", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }; 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (mHelper != null) mHelper.dispose(); 
     mHelper = null; 
    } 
} 

當我在我的按鈕,點擊它說:「支付成功」之後,當我點擊我的按鈕,再不會做任何事情。

我有2個問題:

  1. 如何購買一個應用程序內購買多次嗎?
  2. 如何在支付成功後顯示Toast?

感謝您的回答。

回答

1
  1. 購買必須在每次購買後消耗,然後可以再次購買。如果您能夠購買一次,但不能再次購買,那麼幾乎可以肯定的是,問題在於您沒有正確使用它。

  2. 你的代碼似乎沒有任何問題,Toast.makeText(...)應該可以正常工作。

+0

我該如何正確使用它? – Tms852

+0

調試您的代碼並查看正在報告的消息,然後您會看到它失敗的位置。我建議使用System.out.println或同等功能,並從adb中讀取消息,而不是使用Toasts進行調試。祝你好運! – TopCog

+0

另外,購買完成後無需查詢庫存。您可以立即從purchaseFinishedListener消費該物品。 – TopCog