1

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

user1026605
  • 1,633
  • 4
  • 22
  • 58

1 Answers1

-1

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:

  1. It has a visible activity, whether the activity is started or paused.
  2. It has a foreground service.
  3. 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

Community
  • 1
  • 1
Muhammad waris
  • 314
  • 1
  • 10