你应该在 `onBillingServiceDisconnected()` 中放置什么?

huangapple go评论85阅读模式
英文:

What should you put in `onBillingServiceDisconnected()`?

问题

这是我得到的方法:

public void setupBillingClient() { //连接到谷歌商店

    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) {
                // 账单客户端设置成功
                loadAllSkus();
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            // TODO:实现重试逻辑,以处理与谷歌商店的连接丢失,通过调用startConnection()再次连接
        }
    });
}

Google表示我应该 "实现重试逻辑",但并未说明具体方法。我认为也许可以在 onBillingServiceDisconnected() 中调用 setupBillingClient(),但有人说这会导致崩溃。此外,如果问题如此简单,那么谷歌可能会直接告诉我们这么做,而不是给出模糊的指示来实现重试逻辑。

英文:

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.

答案1

得分: 5

我也遇到了这个问题。谷歌关于这个问题的文档就像API本身一样混乱。

所以,在这里1谷歌说

为了实现重试逻辑,重写onBillingServiceDisconnected()回调方法,并确保BillingClient在进行进一步请求之前调用startConnection()方法以重新连接到Google Play。

这意味着在断开连接后,我们必须手动调用startConnection

但是在这里2谷歌说

用于通知连接到计费服务的连接丢失。

注意:这不会移除计费服务的连接本身 - 绑定到服务的绑定将保持活动状态,并且在计费服务下次运行并设置完成时,您将收到调用onBillingSetupFinished(BillingResult)的通知。

在我看来,这绝对与前面的说法相矛盾。

根据我使用计费库的经验,我认为最后一种说法更有可能是正确的。虽然我不是100%确定。

但是我可以确认,在logcat中我看到了一个断开连接的消息,后面是一个计费客户端已准备好的消息。不过,我没有执行任何重新启动操作。此外,如果我在断开连接的回调中尝试startConnection,那么每次连接/断开连接时我都会在logcat中收到两条消息。

基于此,我可以说:
1)您可以在3此处点击页面底部的“没有帮助”,或在Twitter上标记他们,或在其跟踪器上创建问题。
2)我们正在讨论的重试逻辑不是关于重新连接。而是关于重试使用计费客户端尝试执行的操作,但由于断开连接而未能成功执行的操作。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月7日 23:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64247870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定