Android Kills Background Services in Xiaomi, Huawei etc

后端 未结 2 1015
[愿得一人]
[愿得一人] 2021-01-25 00:53

I need to make my service unstoppable. I tried to return START_STICKY on start command, it works well on emulator but, when remove the app from task manager on my device (Xiaomi

相关标签:
2条回答
  • 2021-01-25 01:32

    Please take a look here. I had same problem and, after lots of research, I figured it out: You must white-list your app.

    0 讨论(0)
  • 2021-01-25 01:42

    There are some customizations on Chinese roms regarding app lifecycle.

    Xiaomi kills foreground services indiscriminately (xiaomi redmi note4 (Android N), note 7 (Android P)). So it's really difficult to have the same behavihour on all phones.

    You can try to relaunch your foreground service using an alarm with a broadcast receiver. When you app is being killed you need to set this alarm. When the intent is received try to start the foreground service with startForeground(). Remember also to bind the foreground service and disable battery limitation (and others things related) according to your device.

    I don't know if this works because we have no necessity to implement such a method. But you could give it a try.

    Remember that for Android >= Oreo the only way for a service to survive and works seems to be the foreground service way.(Bound service with battery optimization off for some devices)

    You can find useful material at:

    Never Ending background service

    LocationUpdatesForegroundService

    The second example works on emulator on different version of android(5/7). It works on Galaxy S3 with Resurrection Remix rom (Android 7) and works on some Huawei models (Oreo, Pie).

    Work with services in android is really difficult. Have patience and go on.

    0 讨论(0)
提交回复
热议问题