安卓计费异常

23
我正在测试我的计费系统,但出现了以下异常:
java.lang.IllegalStateException: Can't start async operation (launchPurchaseFlow) because another async operation(launchPurchaseFlow) is in progress.
        at utils.IabHelper.flagStartAsync(IabHelper.java:711)
        at utils.IabHelper.launchPurchaseFlow(IabHelper.java:316)
        at utils.IabHelper.launchPurchaseFlow(IabHelper.java:294)
        at com.problemio.SubscribeIntroActivity$6.onClick(SubscribeIntroActivity.java:117)
        at android.view.View.performClick(View.java:2532)
        at android.view.View$PerformClick.run(View.java:9308)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:4293)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

我运行了这段代码:

    Button subscribe = (Button)findViewById(R.id.subscribe);
    subscribe.setOnClickListener(new Button.OnClickListener() 
    {  
       public void onClick(View v) 
       {              
           // FIRST CHECK IF THE USER IS ALREADY A SUBSCRIBER.
          mHelper.launchPurchaseFlow(SubscribeIntroActivity.this, SUBSCRIBE_SKU, RC_REQUEST, mPurchaseFinishedListener);

       }
    });   

在此之前,我以测试用户身份运行了它,并使用了测试产品ID:android.test.purchased,它可以正常工作。但是当我将产品ID更改为自己的产品ID之一时,就会崩溃并出现上述异常。

有什么想法是为什么会发生这种情况吗? 谢谢!


谢谢,但是如何重现这个错误呢?你能否提供一下步骤?用户报告了这个问题,但我无法重现。 - Goofy
4个回答

55

IabHelper只允许同时执行一个异步查询。你需要实现onActivityResult()并将参数传递到IabHelper的handleActivityResult()方法中。

应用内购买示例代码实现该方法如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.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.");
    }
}

为了测试购买真实物品,您需要将应用程序的版本上传到开发者控制台(但不需要发布它),并且您必须使用相同的(已签名)版本进行测试。Google Play还需要一些时间来处理/传播新添加的项目...所以有时候您只需要等待。如果您还没有,请查看http://developer.android.com/training/in-app-billing/test-iab-app.html。 - ashughes
8
onActivityResult()并没有解决问题,第二次点击购买仍然会出现异常。 - Muhammad Babar
1
我也在使用样例,并且得到了相同的异常。onActivityResult()方法绝对被调用了。 - user291701
1
我已经实现了这个,但仍然出现异常,还有其他解决方案吗? - Goofy
这段优雅的代码比onActivityResult()的简单驱动程序或模板代码实现要好得多。 - Androidcoder
显示剩余7条评论

2

如果有人像我一样看不清问题的根本原因...

在Play开发者控制台中,我收到了一个java.lang.IllegalStateException堆栈跟踪,它没有提供比错误消息更多的信息... 所以我被难住了。

起初我无法弄清楚这是怎么发生的,因为我从来没有想过尝试两次点击触发IAB的按钮!(由于一个允许点击穿透的覆盖层,有时第一次点击后它看起来是禁用的)。

所以,请确保您的用户不能连续点击按钮。


1
您正在使用 Google 的示例代码,在 IabHelper 类的第 793 行有以下代码。
 if (mAsyncInProgress) throw new IllegalStateException("Can't start async operation (" +
            operation + ") because another async operation(" + mAsyncOperation + ") is in       progress.");

当您首次购买时,“mAsyncInProgress”变为true,并且在您未使用购买之前它将保持为true,因此您需要使用购买。

我建议您完全阅读util包中的所有类,这将有助于您。

任何成功购买后,您需要使用它。

mHelper.consumeAsync(purchase, mConsumeFinishedListener)

但有时候购买请求会失败,因此您需要在每次创建活动时处理您的购买:
mHelper.queryInventoryAsync(mGotInventoryListener);

尝试在 mGotInventoryListener 回调中使用您的购买物品。

1
消费购买是一种计费模式。当您希望允许用户重新购买同一产品时,您可以进行消费。在这里,我们遇到了一个问题,即当我们发出购买请求后旋转手机(或在这种情况下第二次点击购买按钮),并且在第一个请求仍在等待时发出新的购买请求。 - eugene

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接