Android 9 (Pie) Only: Context.startForegroundService() did not then call Service.startForeground() - Works fine on Oreo

后端 未结 3 1402
陌清茗
陌清茗 2021-02-04 03:04

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

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 03:50

    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.

    There are some possibilities:

    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.

提交回复
热议问题