【发布时间】:2021-08-13 08:03:12
【问题描述】:
我有一项服务可以跟踪向用户交付的状态。
它有一个这样的 onCreate 方法:
override fun onCreate() {
super.onCreate()
job = Job()
// We must always start foreground as otherwise we can get crashes
startForeground(
NOTIFICATION_ID,
createNotification(deliveryTrackerUseCase.statusData.value)
)
deliveryTrackerListener.setListeningIfNecessary(true)
wakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DeliveryTrackerService::lock")
combine(
deliveryTrackerUseCase.statusData,
orderUpdatesRepository.orderUpdatesEnabled
) { trackerStatus, updatesEnabled ->
if (updatesEnabled) {
notificationManager.notify(NOTIFICATION_ID, createNotification(trackerStatus))
}
}.launchIn(coroutineScope)
}
但是我仍然在 firebase crashlytics 中看到以下崩溃:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{4f65483 u0 com.example.engine.DeliveryTrackerService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2389)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8393)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
我到底该怎么做才能阻止这种崩溃?这用于在协程范围内设置通知,但我已将其移至 onCreate 函数的顶部以避免此问题,但我们仍然看到崩溃。
编辑:这似乎只在华为设备上,所以也许这与它有关。
【问题讨论】:
-
stackoverflow.com/questions/44425584/…有没有检查过同样报错的链接和解决方法共享?
标签: android android-service foreground-service crashlytics-android