Skip to content

Instantly share code, notes, and snippets.

@kibao
Created March 16, 2017 09:35
Show Gist options
  • Save kibao/38112ad5c8c17895fc661e06b69e6a75 to your computer and use it in GitHub Desktop.
Save kibao/38112ad5c8c17895fc661e06b69e6a75 to your computer and use it in GitHub Desktop.
Is Application Foreground?
class Helper {
public static boolean isAppForeground(Context context) {
KeyguardManager keyguardManager =
(KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
if (keyguardManager.inKeyguardRestrictedInputMode()) {
return false;
}
int myPid = Process.myPid();
List<RunningAppProcessInfo> runningAppProcesses =
((ActivityManager) context.getSystemService(ACTIVITY_SERVICE)).getRunningAppProcesses();
if (runningAppProcesses == null) {
return false;
}
for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
if (runningAppProcessInfo.pid == myPid) {
return runningAppProcessInfo.importance == IMPORTANCE_FOREGROUND;
}
}
return false;
}
}
@omarkimani
Copy link

with some revision the application will be foreground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment