First, add the GET_TASKS permission to your AndroidManifest.xml
< uses-permission android:name = "android.permission.GET_ACCOUNTS" /> |
private 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; } |
Good luck!
0 comments:
Post a Comment