0

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)?

UFC Insider
  • 838
  • 1
  • 7
  • 19
  • `onTaskRemoved()` is not guaranteed to be called. Maybe [here](http://stackoverflow.com/questions/35729654/whi%D1%81h-function-is-called-when-application-is-removed-from-task-manger) you might find some help. – Onik Mar 22 '16 at 21:23
  • This is called when I need it (I can see it in the Log cat)... But for some reason the boolean "isAppInBackground" does not change and I don't know why – UFC Insider Mar 22 '16 at 21:34
  • 1
    If a process has been terminated by OS, you have...nothing! in memory. When OS starts it again later `isAppInBackground` will be set to a default value. – Onik Mar 22 '16 at 21:40
  • 2
    Going into the background and being killed are two different things. If your other service is running in the same process, it will also be killed. If it's running in a different process, it has its own independent copy of the static variable. – Kevin Krumwiede Mar 22 '16 at 21:40
  • But even setting a sharedPreferences value did not work! How can I notify my other Service then? – UFC Insider Mar 22 '16 at 21:41
  • look at here : http://stackoverflow.com/questions/24127923/service-ontaskremoved-not-called-if-started-with-bindservice – Saeid Apr 19 '17 at 09:27

0 Answers0