android check if app is running 1

android check if app is running

private fun isAppRunning() : Boolean {
        val services = (getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).runningAppProcesses
        return services.firstOrNull{it.processName.equals(packageName,true)} != null

Here is what the above code is Doing:
1. We are creating a BroadcastReceiver which will listen to the ACTION_SCREEN_OFF event.
2. We are creating a service which will start the BroadcastReceiver.
3. We are creating a method which will check if the app is running or not.

Now, we need to register the BroadcastReceiver in the manifest file.

Similar Posts