【问题标题】:ANR - Context.StartForegroundServiceANR - Context.StartForegroundService
【发布时间】:2018-08-19 16:57:37
【问题描述】:

我有一个具有前台服务的应用,但我偶尔会收到此 ANR:

Context.startForegroundService() 没有调用 Service.startForeground()

但是,我检查了我的代码,但我没有在任何地方打电话给startForegroundService()。将我的 Service 绑定到我的主 Activity 时仅 StartService。

还有什么可以调用Context.StartForegroundService()的吗?

我已经尝试通过在我的服务 onCreate 和 onStartCommand 中添加 startForeground 来缓解这种情况。但它仍然会继续弹出。

编辑:这里是合并的清单服务:

     <service
        android:name="com.squareup.leakcanary.internal.HeapAnalyzerService"
        android:enabled="false"
        android:process=":leakcanary" />

     <service
        android:name="com.squareup.leakcanary.DisplayLeakService"
        android:enabled="false"
        android:process=":leakcanary" />
      <service
            android:name="com.google.firebase.messaging.FirebaseMessagingService"
            android:exported="true" >
            <intent-filter android:priority="-500" >
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
      </service>
      <service
            android:name="com.google.firebase.iid.FirebaseInstanceIdService"
            android:exported="true" >
            <intent-filter android:priority="-500" >
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
      </service>
      <service
            android:name="com.google.android.gms.measurement.AppMeasurementJobService"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />

      <service
            android:name="com.google.android.gms.measurement.AppMeasurementService"
            android:enabled="true"
            android:exported="false" />

【问题讨论】:

  • 您使用的具有服务的库可能使用startForegroundService()。在 Android Studio 的清单编辑器中检查 Merged Manifest 子选项卡,看看是否有 &lt;service&gt; 元素不指向您自己的类。
  • @CommonsWare 感谢您的回复-我刚刚编辑了原始帖子,其中包含的所有内容都不是我自己的服务类。如果有任何问题,请告诉我。
  • 确保您使用的是最新版本的库。例如,Leak Canary 已经针对这些 Android 8.0+ 的变化进行了一些与服务使用相关的更新。
  • Leak Canary 是最新版本,Firebase 也是如此。唯一过时的是 android 支持库,我从 27.0.2 移动到 27.1.0
  • 嗯...在 LogCat 中向后滚动并查找有关正在为您的应用启动的服务的调试/信息消息。它应该在您正在寻找的错误的 10-15 秒内。

标签: android


【解决方案1】:

startForegroundService(Intent service)abstract 方法。 类似于startService(Intent),但有一个隐含的承诺,即服务一旦开始运行就会调用startForeground(int, android.app.Notification)see here

【讨论】:

  • 感谢您的回答 - 我知道它是什么。我没有在我的代码中调用它,而且无论如何我已经在尽快调用 startForeground(在 oncreate、onStartCommand 等中)
猜你喜欢
  • 1970-01-01
  • 2012-04-19
  • 2017-11-09
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
相关资源
最近更新 更多