android – inventory.getPurchase()总是返回null,尽管已经购买了

前端之家收集整理的这篇文章主要介绍了android – inventory.getPurchase()总是返回null,尽管已经购买了前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用in-ap购买样本.我的应用程序有2个按钮,第一个按钮默认禁用,点击第二个按钮将购买并启用第一个按钮.购买按钮工作正确,但购买后,我检查库存但它总是返回null,这意味着我还没有购买.

这是源代码

建立:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buyButton = (Button) findViewById(R.id.buyButton);
    clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setEnabled(false);
    String base64EncodedPublicKey = "key";
    mHelper = new IabHelper(this,base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG,"In-app Billing setup Failed: " + result);
            } else {
                Log.d(TAG,"In-app Billing is set up OK");
            }
            // Query to detect user was buy this item or not
            mHelper.queryInventoryAsync(mReceivedInventoryListener);
        }
    });

    buyButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (clickButton.isEnabled()) {
                mHelper.queryInventoryAsync(mReceivedInventoryListener);
            } else {
                buyClick(v);
            }
        }
    });
}

购买项目点击:

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

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,Purchase purchase) {
        if (result.isFailure()) {
            // Handle error
            return;
        } else if (purchase.getSku().equals(ITEM_SKU)) {
            // consumeItem();
            // buyButton.setEnabled(false);
            mHelper.consumeAsync(purchase,mConsumeFinishedListener);
        }

    }
};

听众:

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

        if (result.isFailure()) {
            Toast.makeText(getApplicationContext(),"Query Inventory Error!",Toast.LENGTH_SHORT).show();
            // Handle failure
        } else {
            Toast.makeText(getApplicationContext(),"Query Inventory Success!",Toast.LENGTH_SHORT).show();
            // mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),// mConsumeFinishedListener);
            // if (inventory.hasPurchase(ITEM_SKU)) {
            // clickButton.setEnabled(true);
            // }
            Purchase item = inventory.getPurchase(ITEM_SKU);
            if (item != null) {
                clickButton.setEnabled(true);
            } else {
                Toast.makeText(getApplicationContext(),"This item was not buy yet!",Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase,IabResult result) {

        if (result.isSuccess()) {
            Toast.makeText(getApplicationContext(),"Consume done!",Toast.LENGTH_SHORT).show();
            clickButton.setEnabled(true);
        } else {
            Toast.makeText(getApplicationContext(),"Consume Error!",Toast.LENGTH_SHORT).show();
            // handle error
        }
    }
};

当第一次点击“购买”按钮时,它会显示付款对话框,在付款成功后,启用了clickButton.

但是,当我第二次点击buyButton时,它会转到库存,但inventory.getPurchase(ITEM_SKU)总是返回null.

任何的想法?

谢谢!

解决方法

只需在OnIabPurchaseFinishedListener中注释或删除您的代码“mHelper.consumeAsync(purchase,mConsumeFinishedListener);”

If you prefer Android default Trivial demo then go for the premium
product purchase for the single time product purchase.

OnIabPurchaseFinishedListener()

// Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,Purchase purchase) {
            Log.d(TAG,"Purchase finished: " + result + ",purchase: "
                    + purchase);
            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                // setWaitScreen(false);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification Failed.");
                // setWaitScreen(false);
                return;
            }

            Log.d(TAG,"Purchase successful.");


                if (purchase.getSku().equals(SKU_PREMIUM)) {
                          // bought the premium upgrade! 
                          Log.d(TAG,"Purchase is premium upgrade. Congratulating user.");
                  alert("Thank you for upgrading to premium!");
                    }

        }
    };

Explaination why it gives null data :

Keep it mind that Google will store data from their side only when you
are trying to purchase one time purchase product. But Whenever you
purchased consumable item google play store will not be managed it’s
product purchased detail and other things in the Google play console.
That’s why we have to call consumeAsync() method. when we purchased
item,Google play store keep record item has been purchased for the
one time and allow you to purchased second time.

欲获得更多信息 :
android: Inapp billing: Error response: 7:Item Already Owned

但是,在这里您尝试购买产品作为一次性购买,因此您不必在onIabPurchasedFinishListener中调用consumeAsync()方法.

希望它能解决你的问题.

原文链接:/android/309140.html

猜你在找的Android相关文章