Android in-app billing: Can't start async operation because another async operation (is in progress)

前端 未结 19 672
星月不相逢
星月不相逢 2020-12-07 10:34

I am using the IabHelper utility classes, as recommended by Google\'s tutorial, and I\'m being hit hard by this error. Apparently IabHelper can not

相关标签:
19条回答
  • 2020-12-07 10:58

    I have had this issue occasionally, and in my case I've tracked it down to the fact that if the onServiceConnected method in IabHelper can be called more than once if the underlying service disconnects and reconnects (e.g. due to an intermittent network connection).

    The specific operations in my case were "Can't start async operation (refresh inventory) because another async operation(launchPurchaseFlow) is in progress."

    The way that my app is written, I can't call launchPurchaseFlow until after I've had a completed queryInventory, and I only call queryInventory from my onIabSetupFinished handler function.

    The IabHelper code will call this handler function whenever its onServiceConnected is called, which can happen more than once.

    The Android documentation for onServiceDisconnected says:

    Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder) when the Service is next running.

    which explains the problem.

    Arguably, IabHelper shouldn't call the onIabSetupFinished listener function more than once, but on the other hand it was trivial to fix the problem in my app by simply not calling queryInventory from within this function if I've already done it and got the results.

    0 讨论(0)
  • 2020-12-07 11:00

    My solution is simple

    1.) Make the mAsyncInProgress variable visible outside of IabHelper

    public boolean isAsyncInProgress() {
        return mAsyncInProgress;
    }
    

    2.) Use this in your Activity like:

    ...
    if (mIabHelper.AsyncInProgress()) return;
    mIabHelper.queryInventoryAsync(...);
    ...
    
    0 讨论(0)
  • 2020-12-07 11:01

    I was having the same issue until I stumbled upon another SO thread. I'm including a touched up version of the code found in the other thread that you need to include in your Activity that initialises the purchase.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        // Pass on the activity result to the helper for handling
        // NOTE: handleActivityResult() will update the state of the helper,
        // allowing you to make further calls without having it exception on you
        if (billingHelper.handleActivityResult(requestCode, resultCode, data)) {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
            handlePurchaseResult(requestCode, resultCode, data);
            return;
        }
    
        // What you would normally do
        // ...
    }
    
    0 讨论(0)
  • 2020-12-07 11:02

    I have same issue, but it resolved! I think you must be not run "launchPurchaseFlow" on UI thread, try to run launchPurchaseFlow on UI thread, it would be working fine!

    mActivity.runOnUiThread(new Runnable(){
                public void run(){
                    mHelper.launchPurchaseFlow(mActivity, item, 10001, mPurchaseFinishedListener,username);
                }
            });
    
    0 讨论(0)
  • 2020-12-07 11:03

    Or, you can get the latest IabHelper.java file here: https://code.google.com/p/marketbilling/source/browse/

    The March 15th version fixed this for me. (Note other files with no changes were committed on the 15th)

    I still had to fix one crash that happened during testing caused by a null intent extras when "android.test.canceled" was the sent SKU. I changed:

    int getResponseCodeFromIntent(Intent i) {
        Object o = i.getExtras().get(RESPONSE_CODE);
    

    to:

    int getResponseCodeFromIntent(Intent i) {
        Object o = i.getExtras() != null ? i.getExtras().get(RESPONSE_CODE) : null;
    
    0 讨论(0)
  • 2020-12-07 11:04

    Find flagEndAsync() inside IabHelper.java file and make it public function .

    Before trying purchase call flagEndAsync() for your IabHelper .

    you must do somthig like this code :

    mHelper.flagEndAsync();
    mHelper.launchPurchaseFlow(AboutActivity.this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "payload-string");
    
    0 讨论(0)
提交回复
热议问题