Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to handle BillingClient.onBillingServiceDisconnected()?

Posted on November 14, 2022

Solution 1 :

Didn’t find a concrete answer to my question how to handle the failure case. I refactored my code so I basically ignore a call to onBillingServiceDisconnected() and only show an error message to the user.

Each call to attempting to make a purchase now checks if

  • BillingClient is initiliazed
  • BilligClient is ready
  • Sku details are not empty

And only after these succeed try to make the purchase.

Previously I did all of above on app init once. If connection fails, now, I will simply retry when the user clicks on the purchase item again (with a try catch). This maybe doesn’t fix the crash issue but at least gives the user a better experience and control.

Solution 2 :

I’ve also been receiving a similar stack trace in my Google Play Dashboard, however not as a crash but an ANR. The way I solved it was by moving the call to reinitialize the billing to a background thread.

Note that onBillingServiceDisconnected will be called when there’s a connection, but it gets lost. You can test it by clearing Google Play’s data while your app is open. If you don’t retry at this point the connection will be lost.

While onBillingSetupFinished with an error code will be called when you are trying to connect, but it failed. No connection existed beforehand. Confusingly enough you should also retry here depending on the error code.

Solution 3 :

You can handle onBillingServiceDisconnected() menthod like this way by implement a Retry logic:

private static long reconnectMilliseconds = 1000;

@Override
public void onBillingServiceDisconnected() {
    retryBillingServiceConnection();
}

private void retryBillingServiceConnection() {
    new Handler().postDelayed(() ->
                    connectToBillingService(),
            reconnectMilliseconds);
    reconnectMilliseconds = reconnectMilliseconds * 2;
}

Problem :

Recently I migrated one of my apps to Google Play In-App Billing v3. Since the release I get some crash reports on Samsung devices only, which are all related to BillingClient.onBillingServiceDisconnected() being called.

READ  [ANSWERED] gradle - Failed to compile unsupported use of invokespecial
Powered by Inline Related Posts

Current code looks like this:

val billingClient = BillingClient.newBuilder(context)
            .setListener(updatedListener)
            .enablePendingPurchases()
            .build()

billingClient.startConnection(
            object : BillingClientStateListener {
                override fun onBillingSetupFinished(billingResult: BillingResult) {
                    if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                        // The billing client is ready. You can query purchases here.
                        querySkuDetails()
                    }
                }

                override fun onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    initBilling() // all code here is wrapped in this method
                }
            }
)

where I obviously re-initialize the BillingClient and call startConnection() again in error case. The crash then is

java.lang.IllegalStateException: 
  at android.os.Parcel.createException (Parcel.java:2096)
  at android.os.Parcel.readException (Parcel.java:2056)
  at android.os.Parcel.readException (Parcel.java:2004)
  at android.app.IActivityManager$Stub$Proxy.registerReceiver (IActivityManager.java:5557)
  at android.app.ContextImpl.registerReceiverInternal (ContextImpl.java:1589)
  at android.app.ContextImpl.registerReceiver (ContextImpl.java:1550)
  at android.app.ContextImpl.registerReceiver (ContextImpl.java:1538)
  at android.content.ContextWrapper.registerReceiver (ContextWrapper.java:641)
  at com.android.billingclient.api.zze.zza (zze.java:5)
  at com.android.billingclient.api.zzd.zza (zzd.java:5)
  at com.android.billingclient.api.BillingClientImpl.startConnection (BillingClientImpl.java:58)
  at de.memorian.gzg.presentation.base.IAPHelper.initBilling (IAPHelper.java:40)
  at de.memorian.gzg.presentation.base.IAPHelper$initBilling$1.onBillingServiceDisconnected (IAPHelper.java:53)
  at com.android.billingclient.api.BillingClientImpl$zza.onServiceDisconnected (BillingClientImpl.java:11)
  at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:2060)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:2099)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:7857)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1076)
Caused by: android.os.RemoteException: 
  at com.android.server.am.ActivityManagerService.registerReceiver (ActivityManagerService.java:16726)
  at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:2250)
  at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:3357)
  at android.os.Binder.execTransactInternal (Binder.java:1021)
  at android.os.Binder.execTransact (Binder.java:994)

I was wondering what I’m doing wrong within onBillingServiceDisconnected(), so I googled some time and didn’t find any clear advise but // implement your own retry logic. That’s e.g. what Google says. What exactly is the retry logic here? As you see in the stacktrace calling startConnection() again, as suggested by Google’s comment, leads to the crash. Here Google says that I should ignore it since Play Services will call onBillingSetupFinished() eventually, later.

How do you handle this case?

Comments

Comment posted by jomin v george

Did you come across any perfect solution to this?

READ  [ANSWERED] android - Flutter || Padding for floatingActionButton in flutter
Powered by Inline Related Posts

Comment posted by Artem Mostyaev

adb shell pm clear com.android.vending

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress