Created
July 6, 2022 16:55
-
-
Save mahdi-malv/648f6184690cc0ab8422b05bf4c043dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static boolean isApplicationForeground(Context context) { | |
KeyguardManager keyguardManager = | |
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); | |
if (keyguardManager != null && keyguardManager.isKeyguardLocked()) { | |
return false; | |
} | |
ActivityManager activityManager = | |
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
if (activityManager == null) return false; | |
List<ActivityManager.RunningAppProcessInfo> appProcesses = | |
activityManager.getRunningAppProcesses(); | |
if (appProcesses == null) return false; | |
final String packageName = context.getPackageName(); | |
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | |
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND | |
&& appProcess.processName.equals(packageName)) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment