22

如果我通过标准发送购买意向

String mySku = "android.test.purchased";

mHelper.launchPurchaseFlow(this, mySku, 10001, mPurchaseFinishedListener);

我可以购买该物品,它会存储,我可以查询该物品,而且效果也很好。唯一不起作用的是 PurchaseFinishedListener。我的代码与示例应用程序几乎相同,但它似乎根本没有被调用。

12-12 01:40:47.248: D/IabHelper(23502): Calling getPurchases with continuation token: null
12-12 01:40:50.116: D/IabHelper(23502): Starting async operation: launchPurchaseFlow

这些是最后两个被调用的方法,之后当我完成购买时它不会调用 purchasefinishedlistener

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() {

    @Override
    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 (purchase.getSku().equals(mySku)) {
            Log.d(TAG, "mySku is being consumed.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
        }
        Log.d(TAG, "Purchase successful.");
        new AsyncQuestionsActivity().doInBackground();
    }
};

从日志到最后没有任何效果。有什么我只是想念的东西吗?

4

4 回答 4

50

我发现了如何解决它。在onActivityResult 中实现handleActivityResult。它需要在听众和已启动的购买流程之间架起一座桥梁。

下面给出的是我使用的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
                + data);

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        } else {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
        }
    }
于 2012-12-12T04:10:45.490 回答
5

AndroidPenguin,我遇到了和你一样的问题,但是我正确设置了活动结果,但我的 purchasefinishedlistener 没有执行。

这是我的 onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (SettingsManager.getBooleanSetting(rootId, "inapppurchase", false)){
        Log.d(TAG, "take care of activity result with billing helper");
        if (!mBillingService.handleActivityResult(requestCode, resultCode, data)) {
            // not handled, so handle it ourselves (here's where you'd
            // perform any handling of activity results not related to in-app
            // billing...
            super.onActivityResult(requestCode, resultCode, data);
        }
        else {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
        }
    }
    else
        super.onActivityResult(requestCode, resultCode, data);
}
于 2013-01-12T19:40:31.730 回答
1

就我而言,它正在像这样尝试,等到谷歌播放对话框自行关闭。然后,监听器将被调用

于 2013-08-01T11:47:22.843 回答
-1

对于那些与我遇到同样问题的人......

我刚刚将我的成员变量 IabHelper 设为静态,这解决了这个问题。

我已经添加了 AndroidPenguin 的代码,并且在我购买了一个项目之后仍然从一个空的 IabHelper 崩溃(所以 onActivityResult 从来没有机会调用 handleActivityResult,因为 mHelper 是空的)。

于 2014-11-14T16:46:39.033 回答