Android service not restarted

2019-07-28 19:27发布

问题:

I have a service that tracks the user locations. It doesn't have to run 24x7 but it should run for the duration of the trip. Following are the things that I have implemented till now to ensure it's continuous functioning :

  • started service as foreground
  • used "START_STICKY"
  • started an alarm which checks every 10 mins if service is running. If not it starts service.

Despite of all these measures, for some devices(like MI 3w) service gets killed within 10 mins and doesn't restart at all(not even after waiting for 12 hours!). From extensive googling I have learned that when process is killed by OS the alarm doesn't trigger(correct me if I am wrong), so this might be the case here. But what happened to "START_STICKY"?

So my queries are :

  • What happened to START_STICKY? Is it an OS bug (OS V6)?
  • Why does OS cancel all the set alarm while killing the process?
  • Most importantly how to ensure that my service runs for the trip duration?

回答1:

Some devices(like Mi 3w)

What happened to START_STICKY? Is it an OS bug (OS V6)?

Nothing. Xiaomi does not allow the App to auto start. No bug, it's a Memory management technique of Xiaomi.

Conclusion

It doesn't new on Xiaomi because Xiaomi has a feature called app permission, where a user has to allow the app to start automatically (Service).

Go like this and allow your app to autostart:

Settings > permissions > Autostart