关于应用内购买:Android

时间:2014-02-07 13:19:03

标签: android in-app-purchase

我正在使用应用内购买我的测验应用程序购买硬币。我已经分别添加了2个硬币类别100个硬币和500个硬币。我还添加了2个SKU。我的两个硬币购买效果都很好。我购买100个硬币和消费.100个硬币被添加到应用程序,但当我再次购买500个硬币时,100个硬币被添加而不是500.我真的需要一个帮助,因为我坚持这几周。这是消费应用购买代码

private void update() {
    ArrayList<String> moreSkus = new ArrayList<String>();
    moreSkus.add(SKU);
    moreSkus.add(SKU_500);

    buyHelper.queryInventoryAsync(true, moreSkus, new QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
            if(result.isSuccess()) {
               // SkuDetails details = inv.getSkuDetails(SKU);
                //String price = details.getPrice();

                //TextView tvPrice = (TextView)GameActivity.this.findViewById(R.id.textview_price);
               // tvPrice.setText(price);

                purchase = inv.getPurchase(SKU);
               // purchase = inv.getPurchase(SKU_500);
                purchase=inv.getPurchase(SKU_500);
                if(purchase!=null) {
                 //   buy100coins.setEnabled(false);
                    //coins_one_hundred.setVisibility(View.GONE);
                    // buy100coins.setVisibility(View.GONE);


                    buy100coins.setEnabled(false);
                    buy500coins.setEnabled(false);


                   //boughtcoins.setEnabled(true);
                   //boughtcoins.setVisibility(View.VISIBLE);
                    boughtcoins.setEnabled(true);

                } else {
                  //  buy100coins.setEnabled(true);
                    //coins_one_hundred.setVisibility(View.VISIBLE);
                    // buy100coins.setVisibility(View.VISIBLE);
                    buy100coins.setEnabled(true);
                    buy500coins.setEnabled(true);

                    //boughtcoins.setEnabled(false);
                    //boughtcoins.setVisibility(View.INVISIBLE);
                    boughtcoins.setEnabled(false);

                } 

                Toast.makeText(GameActivity.this, "Successful got inventory!", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(GameActivity.this, "Error getting inventory!", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

和消费按钮

               boughtcoins = (Button) buycoinsdialog.findViewById(R.id.bought_coins);
                        //boughtcoins.setVisibility(View.GONE);
                        // if button is clicked, close the custom dialog
                        boughtcoins.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Helper.playSound(getApplicationContext(), "click");
                                buyHelper.consumeAsync(purchase, new OnConsumeFinishedListener() {

                                    @Override
                                    public void onConsumeFinished(Purchase purchase, IabResult result) {
                                        if(result.isSuccess()) {
                                            Toast.makeText(GameActivity.this, "Coins consumed!", Toast.LENGTH_SHORT).show();
                                            Helper.playSound(getApplicationContext(), "cash");                                      
                                                money += 100;
                                               money_text.setText( "$" + money);

                                            try {
                                                // Small HACK: Give the system some time to realize the consume... without the sleep here,
                                                // you have to press "Update" to see that the item can be bought again... 
                                                Thread.sleep(600);
                                                update();
                                            } catch(Exception e) {
                                                // ignored
                                            }

                                        } else {
                                            Toast.makeText(GameActivity.this, "Error consuming: "+result.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });


                                buycoinsdialog.dismiss();


                            }
                        });

4 个答案:

答案 0 :(得分:0)

看起来无论用户选择什么,按此行增加金钱/硬币数量为100:

money += 100;

您在哪里购买500枚硬币?

答案 1 :(得分:0)

你必须按照以下步骤进行操作

 if(purchase.getSKU() == SKU_500){
money += 500;
}else if(purchase.getSKU() == SKU_100){
money += 100;
}

答案 2 :(得分:0)

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener2 
    = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, 
                   Purchase purchase) 
    {
       if (result.isFailure()) {
          // Handle error
          return;
     }else {
if (purchase.getSku().equals(ITEM_SKU1)) {
                 // do things
    }else if(purchase.getSku().equals(ITEM_SKU2){}
}

  }
};

检查这可能会对你有所帮助

答案 3 :(得分:0)

//点击按钮时调用此方法

 if (mHelper!=null) mHelper.flagEndAsync();
                    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                            mPurchaseFinishedListener, "");
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Please try after a few seconds"+e, Toast.LENGTH_SHORT).show();

按如下方式实现监听器:

 IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
        = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, 
                        Purchase purchase) 
        {
           if (result.isFailure()) {
              // Handle error
              return;
         }      
         else{

    mHelper.consumeAsync(purchase, mConsumeFinishedListener);
        }

       }
    };

//实现“Consume Finished Listener”的监听器,如下所示:

 IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener()
        {
             public void onConsumeFinished(Purchase purchase, 
                   IabResult result) 
             {

                if (result.isSuccess()) 
                {                
                      if(purchase.getSKU().equals("android.test.purchase")){
}else if(purchase.getSKU().equals("android.test.cancel")){
}


                } 
            }
        };