Sticky Service not restarting after RAM full on Xi

2019-07-21 11:15发布

问题:

I have a Sticky Service and i'm stress testing the app by filling the phone's RAM. Once the RAM is full, the service is destroyed. Since it's a sticky service, it gets restarted automatically when the system has enough resources.

But this happens only on stock android devices like Moto/Nexus. Devices like Xiaomi or Lava do not restart the sticky service, even on 5.1

Is anyone aware of such problems with heavily customized ROMs? Any workarounds to schedule a restart of my service when the memory gets low?

回答1:

All stock android devices like works well because they use AOSP ROM's

These devices(HUAWEI,LAVA,XIAOMI) are shipped with pre-installed start managers or energy savers (most of the Huawei devices). If the app is not configured appropriately the services will be killed several seconds/minutes after the screen turns off - or it will not automatically be started after reboot.

Now with the feature called Protected Apps, I tried enabling in my app and still the sticky service is not getting started post the App Kill. also, furher observation is:-- The HUAWEI device does not even hold a foreground service

Now the possible workaround is:--

We can use Alarm Manager class to make sure that the service is running when it's necessary. So this Alarm manager can be used to create a timer which checks from time to time if the service should be running and restart it. This time interval needs to be set considering the possible impact on battery consumption.