Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should you put in `onBillingServiceDisconnected()`?

Tags:

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.

like image 930
User104163 Avatar asked Oct 07 '20 15:10

User104163


1 Answers

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.
like image 176
Victor Cold Avatar answered Oct 11 '22 04:10

Victor Cold