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

不想你离开。 提交于 2019-12-02 22:51:27

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.

Has something changed in foreground services in Pie that I'm missing?

YES Have a look here migration notes of Android 9 / Pie

Change

Foreground service permission

Summary

Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.

UPDATE

related issue in Google Issue Tracker Context.startForegroundService() did not then call Service.startForeground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!