Lifecycle of Android Services differs between phones

204 views Asked by At

I'm trying to keep a Service alive beyond the Activity lifetime.

On two phones (Samsung Galaxy S6 and Sony Xperia Z3 Compact) starting an activity in a different process than the Activity then swipe remove the app while Service is visible and notification is shown keeps the service alive.

On a third phone, Huawei Honor 8 the exact same code destroys the Service and cancels any alarms scheduled immediately.

I've had alarms scheduled every 10 seconds (they get cancelled at once on Huawei and works on Samsung/Sony). And having a LocationListener active while running the service in the background.

Another difference is that priority MAX and ongoing notifications on working phones are not cancellable by swiping them away or clearing, but that too is possible on Huawei.

I've requested ignore battery optimization and other settings without any difference. I've also tried looking for an option to not kill the app while the screen is off, which Huawei seem to do at once too.

What can be done about this scenario to keep the service running after the activity is killed or just not actively running. If on low memory, the activity goes but never the service since it hardly consumes any memory. But with this settings, the service gets killed instantly.

Am I looking at specific manufacture settings that I possible can't predict or control from code?

The only possible solution I can see for this type of problem is to send silent pushes to the system checking if the service is alive very frequently and then restarting it when it should be active (due to user choosing to do so but system killing it anyhow).

1

There are 1 answers

3
Hasif Seyd On

For Huawei Devices there is an issue that you can swipe away the ongoing notifications from the Notification panel. This is registered as an issue as it should not behave like that and has been fixed and would be working fine on the new updates .

Regarding the Service getting killed when you close the task from RecentsActivity, its because Huawei has a System app called HwSystemManager(PhoneManager) which does this task. If you go in PhoneManager and under protected Apps, enable your APP as Protected APP, then it will not kill your services and your App may be alive in the background for a long period of time until and unless there is a Low memory situation.