简体   繁体   English

检查应用内购买

[英]Checking for in-app purchases

Following instructions from: https://developer.android.com/training/in-app-billing/purchase-iab-products.html under Query Purchased Items, I am checking for purchased items. 按照来自https://developer.android.com/training/in-app-billing/purchase-iab-products.html的 “查询购买的物品”中的说明进行操作,我正在检查购买的物品。

My code looks like this: 我的代码如下所示:

    private void lookForPurchases() {
    IabHelper.QueryInventoryFinishedListener lookForPurchasesListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                Log.e(TAG, "Error checking for purchases:" + result.toString());
            }
            else {
                Log.d(TAG, "Processing purchases." + inventory.toString());
                if(inventory.hasPurchase(SKU_LEXCOINS_100)){
                    Purchase purchase = inventory.getPurchase(SKU_LEXCOINS_100);
                    consumeCoinPurchase(purchase);
                }
                if(inventory.hasPurchase(SKU_LEXCOINS_550)){
                    Purchase purchase = inventory.getPurchase(SKU_LEXCOINS_550);
                    consumeCoinPurchase(purchase);
                }
                if(inventory.hasPurchase(SKU_LEXCOINS_1200)){
                    Purchase purchase = inventory.getPurchase(SKU_LEXCOINS_1200);
                    consumeCoinPurchase(purchase);
                }
            }
        }
    };

    Log.i(TAG, "Looking for purchases");
    if(inAppBillingHelper == null) {
        Log.e(TAG, "Null preventing query inventory");
    } else {
        try {
            inAppBillingHelper.queryInventoryAsync(lookForPurchasesListener);
        } catch (IabHelper.IabAsyncInProgressException ex) {
            Log.e(TAG, "Error retrieving purchases.", ex);
        }
    }
}

In the log, though, the last thing I get is "Looking for purchases" with nothing following. 不过,在日志中,我得到的最后一件事是“寻找购买”,没有任何后续操作。 It looks like I should get something on any branch, so, does anyone see what I'm doing wrong? 看来我应该在任何分支上都可以得到一些东西,所以,有人看到我在做什么错吗?

No errors or anything, it just never seems to come back. 没有错误或任何东西,它似乎再也不会回来。

The problem was on the line handler.post deep in IabHelper (Google Code) where it was executing my callback I was passing in. The handler.post was getting called, buy my callback never was. 问题是上线handler.post深在其正在执行我的回调,我传递IabHelper(谷歌代码)的handler.post渐渐调用,买我的回调从来没有。 I'm not sure how that happened. 我不确定这是怎么发生的。

I replaced the handler.post with a AsyncTask and posted the call of my callback in the onPostExecute method to get it run on the UI thread. 我用AsyncTask替换了handler.post,并在onPostExecute方法中发布了我的回调的调用,以使其在UI线程上运行。 This appears to have solved my problem. 这似乎解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM