Created
April 13, 2018 12:46
-
-
Save erfanegtfi/b1e4d921d5c9b3c071ef64174069519a to your computer and use it in GitHub Desktop.
is App In Background
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
public static boolean isAppIsInBackground(Context context) { | |
boolean isInBackground = true; | |
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { | |
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); | |
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { | |
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { | |
for (String activeProcess : processInfo.pkgList) { | |
if (activeProcess.equals(context.getPackageName())) { | |
isInBackground = false; | |
} | |
} | |
} | |
} | |
} else { | |
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); | |
ComponentName componentInfo = taskInfo.get(0).topActivity; | |
if (componentInfo.getPackageName().equals(context.getPackageName())) { | |
isInBackground = false; | |
} | |
} | |
return isInBackground; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment