c# - 应用内购买时出现 android.app.Service 连接泄漏错误

标签 c# android xamarin in-app-purchase in-app-billing

我正在使用 Xamarin Android 开发 Android 应用程序。 我正在使用 James Montemagno 的 InAppBilling 插件。

当我调用PurchaseAsync 方法时,PlayStore 对话框将打开。但在后台我的应用程序卡住了,我没有得到任何结果。

var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.Subscription, "apppayload");

系统日志中存在 android.app.ServiceConnectionLeaked 错误:

02-17 22:13:05.434  LENOVO YT3-X50L Error   8031    ActivityThread  android.app.ServiceConnectionLeaked: Activity md5742c3bd4cdfedb6330d25c53207d662c.ShopActivity has leaked ServiceConnection md57a6f08dbc6561d468b2675b2ac9edab2.InAppBillingImplementation_InAppBillingServiceConnection@2277a40 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1092)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:986)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1303)
at android.app.ContextImpl.bindService(ContextImpl.java:1286)
at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21164)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我仅在 LENOVO YT3-X50L 上收到此错误。在三星手机上它运行良好...

最佳答案

ServiceConnectionLeaked

CrossInAppBilling代码没有考虑到您的 Activity 可能会由于内存压力/要求而被杀死/从内存中清除,并且不会直接执行任何操作来尝试阻止它,应用程序开发人员需要确定何时需要采取特殊操作。

我在低端 Android 设备和新的 Android Oreo Go (<=1MB) 测试设备中经常看到这种情况,但在任何设备上都可能发生,但在 2GB 及以下设备上最明显。

  • 在调用任何外部代码之前刷新/释放尽可能多的内存

    • 专注于释放图像通常是减少内存的最大返回(购买完成后恢复它们)

    • 对于 Oreo Go 512MB 设备,我必须付出额外的努力来完成当前的 Activity,创建一个新的空白/空 transient Activity,然后调用外部代码(应用程序计费、相机等)完成后,恢复原始 Activity 并使用外部获得的新信息使其保持最新。

注意:首先分析您的应用和 Activity ,以便集中您的时间。

  • 使用您自己的保持 Activity 服务

    • 使用 Start/StopService 而不是 Bind/UnBindService
    • 使其成为前台服务

注意:这不会阻止操作系统杀死/刷新您的 Activity/服务,它只是提供一个“提示”,表明它不应该...

注意:监控Importance RunningAppProcessInfo内的状态确定您的应用是否正在输入 ReasonServiceInUse在执行外部代码之前。

关于c# - 应用内购买时出现 android.app.Service 连接泄漏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846134/

相关文章:

c# - CsvHelper - 当前版本的 CsvHelper 中的 .CurrentRecord/.Context.Record 替换

c# - Lambda 表达式 NotContains 运算符是否存在?

android - 如何使用服务帐户 JSON 创建 GoogleCredential

android - 如何在 Android 中为通知区域和通知抽屉使用不同的通知图标

c# - 从 .NET Framework 2.0 迁移到 4.5.2

c# - IIS7 或 ASP.NET 返回一个奇怪的客户端 IP 地址

java - Bundle 丢失两个 Activity 之间的所有数据

android - 我可以将 Firebase 用于只有数据库操作的应用程序吗?

android - Android类库项目中的自定义组件

android - Xamarin.Forms Android 推送通知不出现