如何处理BillingClient.onBillingServiceDisconnected()? [英] How to handle BillingClient.onBillingServiceDisconnected()?

查看:994
本文介绍了如何处理BillingClient.onBillingServiceDisconnected()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我将其中一个应用迁移到了Google Play应用内结算v3.自发布以来,我仅在三星设备上获得一些崩溃报告,这些报告都与BillingClient.onBillingServiceDisconnected()被调用有关.

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.

当前代码如下:

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

我显然在错误的情况下重新初始化了BillingClient并再次调用startConnection().崩溃是

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)

我想知道在onBillingServiceDisconnected()中我做错了什么,所以我搜索了一段时间,但没有找到明确的建议,但// implement your own retry logic.那是例如 Google 说的话.这里的重试逻辑到底是什么?正如您在stacktrace中所看到的,正如Google的注释所建议的,再次调用startConnection()会导致崩溃. 此处 Google说,自Play以来,我应该忽略它服务最终将在以后调用onBillingSetupFinished().

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.

您如何处理这种情况?

推荐答案

找不到关于如何处理失败案例的具体答案.我重构了代码,因此基本上忽略了对onBillingServiceDisconnected()的调用,只向用户显示错误消息.

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已初始化
  • BilligClientready
  • Sku详细信息不能为空
  • 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.

这篇关于如何处理BillingClient.onBillingServiceDisconnected()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆