Created
March 16, 2017 09:35
-
-
Save kibao/38112ad5c8c17895fc661e06b69e6a75 to your computer and use it in GitHub Desktop.
Is Application Foreground?
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with some revision the application will be foreground