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

前端 未结 19 670
星月不相逢
星月不相逢 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:48

    I had the same issue and the problem was that I didn't implement the method onActivityResult.

    @Override
    protected void onActivityResult(int requestCode,
                int resultCode,
                Intent data)
    {
        try
        {
            if (billingHelper == null)
            {
                return;
            } else if (!billingHelper.handleActivityResult(requestCode, resultCode, data))
            {
                super.onActivityResult(requestCode, resultCode, data);
            }
        } catch (Exception exception)
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:50

    Just check for the onActivityResult requestCode on the activity and if it matches the PURCHASE_REQUEST_CODE you used on the purchase just pass it to the fragment.

    When you add or replace the fragment in the FragmentTransaction just set a tag:

    fTransaction.replace(R.id.content_fragment, fragment, fragment.getClass().getName());
    

    Then on your activity's onActivityResult

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PurchaseFragment.PURCHASE_REQUEST_CODE) {
            PurchaseFragment fragment = getSuportFragmentManager().findFragmentByTag(PurchaseFragment.class.getNAme());
            if(fragment != null) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:52

    if you code in fragment then you this code in IabHelper.java

    void flagStartAsync(String operation) {
        if (mAsyncInProgress) {
            flagEndAsync();
        }
        if (mAsyncInProgress) throw new IllegalStateException("Can't start async operation (" +
                operation + ") because another async operation(" + mAsyncOperation + ") is in progress.");
        mAsyncOperation = operation;
        mAsyncInProgress = true;
        logDebug("Starting async operation: " + operation);
    }
    
    0 讨论(0)
  • 2020-12-07 10:54

    Yes, i am also facing this issue but i resolved this but i resolved using

    IabHelper mHelpermHelper = new IabHelper(inappActivity, base64EncodedPublicKey);
      mHelper.flagEndAsync();
    

    The above method stop all the flags. Its work for me must check

    0 讨论(0)
  • 2020-12-07 10:56

    Make sure that you call the IabHelper's handleActivityResult in the Activity's onActivityResult, and NOT in the Fragment's onActivityResult.

    The following code snippet is from TrivialDrive's MainActivity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
        if (mHelper == null) return;
    
        // 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.");
        }
    }
    

    Update:

    • There is now a In-app Billing Version 3 API (what was the version in 2013?)
    • The code sample has moved to Github. Snippet above edited to reflect current sample, but is logically the same as before.
    0 讨论(0)
  • 2020-12-07 10:58

    A simple trick that did it for me was to create a method in IabHelper:

    public Boolean getAsyncInProgress() {
        return mAsyncInProgress;
    }
    

    and then in your code, just check:

    if (!mHelper.getAsyncInProgress())
        //launch purchase
    else
        Log.d(TAG, "Async in progress already..)
    
    0 讨论(0)
提交回复
热议问题