Search code examples
androidserviceforeground-service

How to check in foreground service if the app is running?


In my app I have foreground service which works differently depending on existance of the application it belongs to.

User can switch to the Recent screen and kick the application out of memory. Since Application class doesn't have onDestroy method, I won't know if it is running.

Is there a way to check in foreground service if application is running?


Solution

  • There is no proper way to do that. One work around you can use is to start a service normally from your activity and overriding onTaskRemoved method. This method will be called when your app is removed from the recent apps screen. You can do set global static variables in your main service class and access them later on to determine whether the app is killed or not.

    This is the service code:

    Your foreground service:

    Kotlin:

    class ForegroundService : Service() {
    
        companion object {
            // this can be used to check if the app is running or not
            @JvmField  var isAppInForeground: Boolean = true
        }
    
        ...
    
    }
    

    Java:

    class ForegroundService extends Service {
    
        public static boolean isAppInForeground = true;
    
    }
    

    Your service for checking app state:

    Kotlin:

    AppKillService.kt

    class AppKillService : Service() {
        override fun onBind(p0: Intent?): IBinder? {
            return null
        }
    
        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
            // service is started, app is in foreground, set global variable
            ForegroundService.isAppInForeground = true
            return START_NOT_STICKY
        }
    
        override fun onTaskRemoved(rootIntent: Intent?) {
            super.onTaskRemoved(rootIntent)
            // app is killed from recent apps screen, do your work here
            // you can set global static variable to use it later on
            // e.g.
            ForegroundService.isAppInForeground = false
        }
    }
    

    Java:

    AppKillService.java

    public class AppKillService extends Service {
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // service is started, app is in foreground, set global variable
            ForegroundService.isAppInForeground = true;
            return START_NOT_STICKY;
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            super.onTaskRemoved(rootIntent);
            // app is killed from recent apps screen, do your work here
            // you can set global static variable to use it later on
            // e.g.
            ForegroundService.isAppInForeground = false;
        }
    }
    

    In your MainActivity:

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // start your service like this
            startService(Intent(this, AppKillService::class.java))
        }
    }