What should you put in `onBillingServiceDisconnected()`?
Asked Answered
K

1

8

Here is the method I've got:

public void setupBillingClient() { //connect to google play
    
        billingClient = BillingClient.newBuilder(context)
                .enablePendingPurchases()
                .setListener(this)
                .build();

        billingClient.startConnection(new BillingClientStateListener() {

            @Override
            public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    //The BillingClient is setup successfully
                    loadAllSkus();
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                //TODO: implement retry logic to handle lost connections to Google Play by calling startConnection() again
            }
        });
    }

Google says I should "implement retry logic" but doesn't say how. I thought maybe to just call setupBillingClient() inside onBillingServiceDisconnected() but some people said that causes a crash. Also I feel if it was that simple then google would have told us to write that instead of the vague instruction to implement a retry logic.

Koren answered 7/10, 2020 at 15:58 Comment(6)
You can use billingClient.startConnection(this); to retry.Gynaecocracy
Also, look at this for retrying only a limited number of times. medium.com/shuttl/…Gynaecocracy
The only question I could find about the matter was this and it says calling startConnection() again causes a crash: #63313549Koren
No solution? :(Koren
Have you tried calling startConnection() again from onBillingServiceDisconnected() a limited number of times. That shouldn't cause any issue and if it still crashes then leave onBillingServiceDisconnected() empty or just log the error.Gynaecocracy
I can't test it. My laptop is too slow for emulation and I already tested the one-time purchase on my only android device. Plus I don't know how to force Android to trigger onBillingServiceDisconnected()Koren
O
6

I also ran into this issue. Google documentation about this is just a mess, (well, like the API itself).

So, here Google says

To implement retry logic, override the onBillingServiceDisconnected() callback method, and make sure that the BillingClient calls the startConnection() method to reconnect to Google Play before making further requests.

Which implies that after the disconnection we have to call startConnection manually.

But here Google says

Called to notify that the connection to the billing service was lost.

Note: This does not remove the billing service connection itself - this binding to the service will remain active, and you will receive a call to onBillingSetupFinished(BillingResult) when the billing service is next running and setup is complete.

Which, in my opinion, absolutely contradicts the previous statement.

From my experience with the billing library, I believe the last statement is more likely to be true. I'm not 100% sure though.

But I can confirm that I saw a disconnect message in the logcat, followed by another message that the billing client was ready. I didn't do any restart actions though. Also, if I tried to startConnection in disconnection callback, then I began to receive two messages in the logcat on each connection/disconnection.

Based on this, I can say that:

  1. You can go here and click on "Not helpful" at the bottom of the page. Or tag them on Twitter, or create an issue on their tracker.
  2. Retry logic, which we are talking about - is not about a connection retry. It's about to retry operation that we tried to perform using the billing client, but it didn't work because it was disconnected.
Oreopithecus answered 30/7, 2021 at 8:51 Comment(1)
Hi sorry for very very late reply. I had lost my account. Can you give an example of what you added inside the onBillingServiceDisconnected() method?Koren

© 2022 - 2024 — McMap. All rights reserved.