In Android O we have some new background limitations. For example we can only register implicit broadcasts via the Context.registerReceiver
method. When the system kills our process (e.g. as a result of low memory) the registered receivers will also be destroyed.
To reduce the chance that the system kills our process we have to tell the system that this process is still in the foreground. As per documentation an app is considered to be in the foreground if any of the following is true:
- 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.
What about TileService
(for quick setting tiles) which were introduced in Android N? When we register a TileService
as ACTIVE_TILE
in the mainfest file the system does not bind the service every time the tile becomes visible (as noted in this article) and therefore our service is bind to another application, in face the system process.
So is my application (as long as the tile is added to the quick settings pane) considered as foreground application? This would be nice because I do not need a persistent notification with this approach and it is nevertheless possible for the user to send my app in the background (by removing the tile)
Not normally. Quoting the article that you linked to:
So, most of the time, your service will be unbound and destroyed.
My guess is that you are referring to these paragraphs:
I would expect there to be some sort of timeout mechanism: if you call
requestListeningState()
, then do not update the tile within X seconds, you will be called withonStopListening()
. The listening state is not durable; it only lives for one update. So, the system should be expecting an update in a timely fashion. I will test this scenario and will file a bug report if aTileService
can be indefinitely bound this way, as that's a waste of system resources.