My application has got two services, ServiceA and ServiceB. ServiceA is a foreground service, ServiceB will be started when the device's screen is on and will be stopped when the device's screen is off.
These two services are collecting information, which we cannot afford to loose. But, on Android 4.4*, when the device's screen is turned off for a while and then turned back on, I can notice 0 processes and ServiceA running in the running section.
I've seen the reports on this version, where they claim that this scenario appears when the user swipes out our application from recent tasks. But, I'm noticing this when the screen is turned off for a while.
Note that ServiceA and ServiceB returns sticky and also, ServiceA is a foreground service.
Any definition will be very helpful.
Related
In my Android app I have a couple of foreground services running (started with START_STICKY, but changing that does not seem to make any difference). When the user quits the app by swiping it away in the task manager I use the onTaskRemoved callback to do some cleanup/syncing in the services and then stop them using stopSelf(). I can verify that all services are stopped in the developer settings.
My problem now is that I would expect the app process to be killed too so any remaining singletons, etc. would get cleaned up and the next time the user launches the app it is in a "clean" state. I can verify that this happens if I don't start any services at all and swipe away the app in the task manager. But as soon as my services come into play the app process does not seem to get killed, even after stopping ALL services.
I am using a Samsung Galaxy Active Tab 3 with Android 12 to test this.
Android determines when, and if, the hosting OS process gets killed. You have no control over when, or even if, this happens. Different manufacturers have different strategies (some are very agressive and kill off processes immediately, some don't).
I have a few questions about certain behavior on Android devices.
I'm using SDK which ask the user to turn-off battery optimization for the app.
I'm also running a foreground service which implements some interfaces from said SDK.
I need the foreground service to run as long as possible with out any other interaction with the app.
What I wanted to know is:
If the user allows to turn off the battery optimization - does it mean that the OS can't kill my foreground service (or it will be killed under some strict conditions).
If the user doesn't allow to turn off the battery optimization - does it mean that the OS will kill my service more easily?
If under some conditions the OS kills my service, the foreground service is also dead, will the service come back to life if I made it START_STICKY and if so, how long does it take it to restart?
Each manufacturer implements Android in a different way, so a specific behaviour seen on (as example) Samsung could not be the same on Xiaomi, and vice-versa. Battery optimization could not involve Services in the way you expect, or maybe yes. It's impossibile to find a fixed rule for this.
(same as 1)
the restart is near-instant, it takes just the time to empty memory, release locks/files and similar things and finally run an "internal startService()" method again.
I'm using a Background Service as the main purpose in my App (it creates some floating windows/interfaces when needed) and I never seen that the OS killed my Service in more than 6 years. However the Service should support to be killed and restarted without FC something.
Im a bit confused on background limitations of apps, and I could use an explanation. So, starting from android 8 we have limitations on services and sending broadcasts. As for now, we can only make service run in background if it has the foreground notification, otherwise it will be killed. The app is considered to be in background if none of it's activities are visible, and my questions are: 1. For how long can the app-process itself live without foreground service? For instance, if I go to home screen, thereby putting app in background my app can still play sounds for hours, but I expected it will be killed by system in a couple of minutes. 2. Is foreground service somehow related to application lifecycle? For instance,maybe if I start the foreground service then my app won't get killed or less likely to be killed by the system.
I'm asking all this because my app is using c++ libs to make VOIP calls and do other stuff in background and I'm wondering what would happen if I just open the home screen and leave my app working, so far I've never seen the system kill the app while the call is active.
For how long can the app-process itself live without foreground service?
Android low memory killer daemon monitors the system constantly. If there is high memory pressure, least essential process gets killed. If there is not a memory problem, your app might live in the background for a long time. However vendors might limit the number of background processes. If this limit is 3 and your app falls to 4th place, it gets killed even if there is no memory pressure. And some vendors just kill the unvisible apps and there is nothing you can do about it. You can check this answer for a similar problem on OnePlus devices.
Is foreground service somehow related to application lifecycle? For instance,maybe if I start the foreground service then my app won't get killed or less likely to be killed by the system.
According to Android Processes and Application Lifecycle documentation foreground services have the 2nd highest priority in the system. So the answer is yes, if you are running a foreground service, your app is less likely to be killed even if you do not have a visible Activity.
I'm developing a player app.
For this reason, it uses a foreground service to handle the playback.
Until recently the service was bound to my activities.
This is not the case anymore.
Since then, some specific devices (mostly Pixel 1/2/3) have been killing my app 1 minute after the screen has been turned off
The service is a foreground service not bound to anything.
Why would the device kill it?
As soon as the app is excluded from the device-optimized apps list the issue is solved
I'm not providing code, because I'm just trying to understand if this situation makes sense and if so what should I do to prevent this
BTW the app is using a receiver to act on Screen_ON/OFF messages. That's how I can see in the logs that the player service onDestroy() method gets killed exactly 1 minute after the screen has been turned off
what should I do to prevent this?
The key point here to keep the service alive is as said in official documentation :
While an app is in the foreground, it can create and run both
foreground and background services freely.
so, we can conclude that keeping the work in foreground and visible to the user has very minimal chances of being killed. And to do so we need to know that how android gets the idea that this process is in foreground ?
Here are the criteria's at which a process is said to be in foreground:
It has a visible activity, whether the activity is started or
paused.
It has a foreground service.
Another foreground app is connected to the app, either by binding to
one of its services or by making use of one of its content
providers. For example, the app is in the foreground if another app
binds to its:
-IME Wallpaper service
-Notification listener
-Voice or text service
If none of those conditions is true, the app is considered to be in
the background.
If none of the above criteria is fulfilled by your app process then thats the reason of your service being killed.
You can read more on this topic here :
Foreground service being killed by Android
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).
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.