谷歌在应用程序计费总是失败,而不是消费

时间:2014-05-22 17:07:36

标签: java android google-play in-app-billing

我试图添加应用结算,允许用户根据自己的喜好进行多次购买(无限制)。因此必须消费。我第一次在新设备上运行代码时工作正常并且测试购买成功。然而,它没有被消费,也没有让我再次购买。问题似乎在于这种方法,因为它总是以result.isFailure()结束,因此购买不被消费(并且只能进行一次)。

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
    {
        if (result.isFailure()) {
            Toast.makeText(getApplicationContext(), "Failed to make purchase.", Toast.LENGTH_LONG).show();
            return;
        }      
        else if (purchase.getSku().equals(ITEM_SKU)) {
            consumeItem();
        }

    }
};

有谁知道如何解决这个问题?

以下是代码的其余部分:

    Preference removeAds = (Preference) findPreference("inAppBilling");
        removeAds.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {

                mHelper.launchPurchaseFlow(About.this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");

                return true; 
            }
    });

    @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(getApplicationContext(), "Failed to make purchase.", Toast.LENGTH_LONG).show();
                return;
            }      
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
            }

        }
    };

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

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                // Handle failure               
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
            }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            if (result.isSuccess()) {                
                CustomAlerts.showBasicAlert("Thanks", "We appreciate your support.", About.this);
            } else {
                // handle error
            }
        }
    };

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

1 个答案:

答案 0 :(得分:1)

您需要在应用开始时查询用户广告资源。如果其中有任何已购买但尚未兑换的产品,您可以在此时兑换。 Here's用于检索它的文档。