Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android billing exception

I am testing my billing and I got this exception:

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)

After I ran this code:

    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);

       }
    });   

But prior to this I ran it as a test user and with the test product id which was this: android.test.purchased and it worked. But when I changed product id to one of my own products ids, it crashed with the exception above.

Any ideas why that happened? Thanks!

like image 567
Oyoyoy Avatar asked Jan 02 '13 22:01

Oyoyoy


People also ask

What is Android app billing?

Google Play's billing system is a service that enables you to sell digital products and content in your Android app. You can use Google Play's billing system to sell a one-time product or subscriptions on a recurring basis.

Can't connect to Google Play billing?

Log out of your Google account Go to Android Settings > Accounts > Google and tap on Remove account. Data will not be lost as normally all of it will be synced with Google's cloud services (true for Google accounts). Re-add your account then restart your device. Try buying again.

What does Google Play in app billing API version is less than 3 mean?

Usually the BILLING_UNAVAILABLE error means that your Android device is running an unsupported version of Android or Play services. Other things to check when you get this error: Are you logged in to the correct Google Account on the device/emulator? Try logging out and logging back in.


3 Answers

The IabHelper will only allow a single asynchronous query to be executed at a time. You need to implement onActivityResult() and pass the parameters into the handleActivityResult() method of the IabHelper.

The in-app billing sample code implements the method like this:

@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.");
    }
}
like image 152
ashughes Avatar answered Nov 09 '22 18:11

ashughes


Just in case someone is missing the forest for the trees like I was...

I received a java.lang.IllegalStateException stack trace in the Play Developer Console which didn't provide much more than the error message... so I was stumped.

I couldn't figure out how this was happening at first because I never thought to try tapping the button that triggers IAB twice! (it looks disabled after the first tap due to an overlay that let's taps through, [sometimes]).

So, make sure your users can't tap your button twice.

like image 31
David Murdoch Avatar answered Nov 09 '22 18:11

David Murdoch


You are using sample code of google and in IabHelper class line 793 there is this piece of code

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

and when you make a purchase for first time 'mAsyncInProgress' becomes true,and until you haven't consumed your purchase it remains true ,so you need to consume your purchase. I recommend you to read all Classes in util package completely,it will help you.

after any successful purchase you need to consume it

mHelper.consumeAsync(purchase, mConsumeFinishedListener)

but sometimes the consume request fails so you need to handle your purchases every time your activity is created :

mHelper.queryInventoryAsync(mGotInventoryListener);

and try to consume your purchases in mGotInventoryListener callback.

like image 1
sadegh saati Avatar answered Nov 09 '22 19:11

sadegh saati