Foreground service getting killed from Oreo

后端 未结 5 794
日久生厌
日久生厌 2020-12-07 22:18

I have written a foreground service which is working properly for all OS version lower than Oreo. From Oreo application process is getting killed after 5 minutes of closing

5条回答
  •  我在风中等你
    2020-12-07 22:23

    I spent several hours solving this issue. Xiaomi phone, Oreo. I have solution, 2 steps:

    1. Xiaomi needs additional permission: Settings->Permissions->Automatic startup. There is a list of applications that are allowed to be started automatically. BOOT_COMPLETED and START_STICKY is NOT enough. Facebook, Outlook, Skype + my app is listed here. I do not know how to ask user programatically to grant this permission.

    2. Service gets recreated but NOT restarted, even if you return START_STICKY. After you kill the app (swipe out from running apps), onCreate is called again but NOT onStartCommand. Try Log.d() to see it. I moved all the logic into onCreate EXCEPT creating the notification, calling startForeground() and returning START_STICKY. This has to remain in onStartCommand().

    Batery optimization must be disabled for the application too, but it was required by previous version of Android already.

    Now my service keeps running or is recreated with 100% success within few seconds.

提交回复
热议问题