I found a way to know when an app goes to background (its task removed). I created a Service
that start as non-sticky and overrided the method:
public class myService extends Service {
...
@Override
public void onTaskRemoved(Intent rootIntent) {
NotificationsListenerTask.isAppInBackground = true;
Log.d("TEST", "background");
stopSelf();
}
}
Where NotificationsListenerTask.isAppInBackground
is a static boolean that's located in another class and set to false by default.
I have another Service
that start as sticky and prints the value of NotificationsListenerTask.isAppInBackground
every 5 seconds.
The problem is, after the app is killed (and I see that onTaskRemoved
is called because the Log.d
that I set appeared), the value of NotificationsListenerTask.isAppInBackground
does not change! It keeps printing the old value every 5 seconds...
Why doesn't it change, and how can I make it change (or how can I notify the other Service that onTaskRemoved was called)?