【问题标题】:Android billing exceptionAndroid 计费异常
【发布时间】:2012-12-17 07:00:32
【问题描述】:

我正在测试我的帐单,但遇到了这个异常:

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 时,它崩溃了,但出现了上述异常。

知道为什么会这样吗? 谢谢!

【问题讨论】:

  • 谢谢,但是如何重现此错误,请您提供步骤,用户正在报告此问题,但我无法重现

标签: android android-billing


【解决方案1】:

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.");
    }
}

【讨论】:

  • 谢谢,是的,我没有意识到这是实现的必要方法。我打电话时有命令吗?还是系统调用的?
  • 当用户从购买界面返回到您的活动时(无论是点击返回按钮、完成购买等),系统都会调用它。然后,IabHelper 将致电您的mPurchaseFinishedListener 并告知购买结果。
  • onActivityResult() 也没有解决问题,点击购买次时仍然出现异常
  • 我也在使用该示例,并得到相同的异常。肯定会调用 onActivityResult()。
  • 我已经实现了这个,但我仍然得到异常,还有其他解决方案吗?
【解决方案2】:

以防万一有人像我一样只见树木不见森林......

我在 Play 开发者控制台中收到了一个 java.lang.IllegalStateException 堆栈跟踪,它提供的只是错误消息......所以我很难过。

一开始我无法弄清楚这是怎么发生的,因为我从未想过要尝试点击两次触发 IAB 的按钮! (在第一次点击后它看起来被禁用了,因为我们可以点击覆盖,[有时])。

因此,请确保您的用户不能点击您的按钮两次。

【讨论】:

    【解决方案3】:

    您正在使用 google 的示例代码,并且在 IabHelper 类第 793 行中有这段代码

     if (mAsyncInProgress) throw new IllegalStateException("Can't start async operation (" +
                operation + ") because another async operation(" + mAsyncOperation + ") is in       progress.");
    

    当您第一次购买时,“mAsyncInProgress”变为真,直到您没有消费您的购买,它仍然是真实的,因此您需要消费您的购买。 我建议你完整阅读 util 包中的所有类,它会对你有所帮助。

    购买成功后需要消费

    mHelper.consumeAsync(purchase, mConsumeFinishedListener)
    

    但有时消费请求会失败,因此您需要在每次创建活动时处理您的购买:

    mHelper.queryInventoryAsync(mGotInventoryListener);
    

    并尝试在 mGotInventoryListener 回调中消耗您的购买。

    【讨论】:

    • 消费购买是一种计费模式。当您希望允许用户重新购买更多相同产品时,您希望消费。在这里,当我们发出购买然后旋转手机(或者在这种情况下第二次单击购买按钮)并发出新的购买请求时,第一个仍在等待中时,我们遇到了问题。
    【解决方案4】:

    在此处获取最新版本的库:https://code.google.com/p/marketbilling/source/browse/ 他们修复了问题的地方

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2021-10-01
      • 2016-06-10
      • 1970-01-01
      相关资源
      最近更新 更多