仅限Android 9(Pie):Context.startForegroundService()然后未调用Service.startForeground()-在Oreo上运行良好 [英] Android 9 (Pie) Only: Context.startForegroundService() did not then call Service.startForeground() - Works fine on Oreo

查看:208
本文介绍了仅限Android 9(Pie):Context.startForegroundService()然后未调用Service.startForeground()-在Oreo上运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为Oreo调整了正在进行的通知,效果很好.现在,仅在Pie(不是在Oreo设备上发生)上,我们得到了标题错误.我缺少的Pie中的前台服务有什么变化吗?

We adjusted our ongoing notification for Oreo and it worked great. Now, on Pie only (not happening on Oreo devices), we're getting the titled error. Has something changed in foreground services in Pie that I'm missing?

这是前台服务的onCreate代码->

Here's the onCreate code for the foreground service ->

override fun onCreate() {
    super.onCreate()

    val notification: Notification = NotificationCompat.Builder(this, packageName)
            .setSmallIcon(R.drawable.status_notification_icon)
            .setContentTitle(getString(R.string.ongoing_notify_temp_title))
            .setContentText(getString(R.string.ongoing_notify_temp_message))
            .setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
            .setColor(ContextCompat.getColor(this, R.color.custom_blue))
            .build()

    startForeground(ONGOING_NOTIFY_ID, notification)

    appSettings = AppSettings(this)

    weatherLookUpHelper = WeatherLookUpHelper()
    MyRoomDatabase.getInstance().invalidationTracker.addObserver(onChange)

    retrieveCurrentLocation()
    createAlarmManager()
}

如您所见,我们只是创建通知,然后调用startForeground.关于为什么此代码会产生标题错误的任何想法?

as you can see, we're just creating the notification and then calling startForeground. Any ideas on why this code would generate the titled error?

侧面注意:Fabric Crashlytics显示此崩溃仅在运行Pie的像素设备(像素,像素xl,像素2,像素2 xl)上发生

Side Note: Fabric Crashlytics shows this crash only happening on Pixel devices (pixel, pixel xl, pixel 2, pixel 2 xl) running Pie

编辑:我们的清单中确实有前台权限

We do have the foreground permission in our manifest

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

推荐答案

如此处所述背景服务限制,应用程序/服务有五秒钟的时间来调用 startForeground(),如果应用程序在该时限内未调用startForeground(),则系统将停止该服务并声明该应用程序成为ANR.

As mentioned here Background Service Limitations, the app/service has five seconds to call startForeground(), If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.

有一些可能性:

  1. 在调用 startForeground()方法之前,您的前台服务都会被破坏/完成.
  2. 或者如果前台服务已经实例化并再次被调用,则不会调用 onCreate 方法,而是会调用 onStartCommand .然后将逻辑移到 onStartCommand 上,以调用 startForeground()方法.
  3. 您在 startForeground 中的通知ID 不得为0 ,否则也会导致崩溃.
  1. Either your foreground service gets destroyed/finished before calling the startForeground() method.
  2. Or if the foreground service is already instantiated and its getting called again, then the onCreate method will not be called, instead onStartCommand will be called. Then move your logic to onStartCommand to call startForeground() method.
  3. Your notification id in startForeground must not be 0, otherwise it will also cause the crash.

这篇关于仅限Android 9(Pie):Context.startForegroundService()然后未调用Service.startForeground()-在Oreo上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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