3

In Marshmallow, even if application in not in backgounnd and foreground . i am getting as "application is running". code i use is

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (int i = 0; i < appProcesses.size(); i++) {
        if (appProcesses.get(i).processName.equals("com.alive.moraribapuapp")) {
            Log.d(TAG, "com.alive.moraribapuapp is true(running)");

            return true;
        } else {
            Log.d(TAG, "com.alive.moraribapuapp is false(Not running)");
            return false;
        }
    }

this giving me true even if application closed in Marshmallow.

Vinay
  • 89
  • 9
  • Are you sure it's closed? Check the settings menu and see if the `Force Stop` option is enabled. If so, it's not closed and is still running. – Ori Lentz Apr 07 '16 at 13:13
  • Ori Lintz, but all application has Force stop enabled, actually i am using this, to hide notification when application is in foreground, like whatsapp. – Vinay Apr 07 '16 at 13:40
  • No, all running applications have Force Stop enabled. Also, you're checking if the app is running, which isn't the same as checking if it's in foreground. Applications living in background are very much alive and running. If you want to know if your app is in foreground, you'll need to know if any activity is currently visual (meaning, if the last activity lifecycle was `onStart` or `onResume` without `onDestroy` or `onPause` called since etc) – Ori Lentz Apr 07 '16 at 13:44
  • rashad.z, problem solved, we manually have to check if aap in is foreground or not. check answer – Vinay May 03 '16 at 07:33

1 Answers1

4

rashad.z, problem solved, we manually have to check if aap in is foreground or not.

 private boolean isAppOnForeground(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;
        }
    }

    Log.d("MyGcmListenerService", "" + isInBackground);
    return isInBackground;
}
Vinay
  • 89
  • 9