-2

I need to check if the app is foreground or background to display incoming notifications either in the tray or in the app. Currently I have done it using RunningTaskInfo but this requires the permission of: android.permission.GET_TASKS which is deprecated. Any help is much appreciated!

My current method of checking

 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;
}

EDIT - The solution

So this is the solution I ended up going with. Thanks to /u/Dima Kozhevin for pointing me to the right post. This is the post: How to detect when an Android app goes to the background and come back to the foreground which I ended up modifying a little. The code I added is to check whether the screen is on or not. I did it the following way:

@Override
public void onActivityPaused(Activity activity) {
    PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
    if (!isInBackground) {
        if (!pm.isScreenOn()) {
            isInBackground = true;
            Log.e(TAG, "app went to background");
        }
    }
}

I ran this solution on 3 different devices which all ended up working. If anyone finds anything that ends up breaking this, please let me know!

CrimzonWeb
  • 171
  • 3
  • 15
  • You might like https://stackoverflow.com/q/4414171/3166697 – Dima Kozhevin Jul 04 '18 at 11:47
  • I've found multiple solutions similar to this, using onResume or things like that but wouldn't that mean I would need to add a check for each of my activities? – CrimzonWeb Jul 04 '18 at 12:20
  • I'm using this solution https://stackoverflow.com/a/30598089/3166697 – Dima Kozhevin Jul 04 '18 at 12:29
  • It half works. I can get into situations where it does not update but it gets me closer. For example If I turn off the screen the app should switch to background but I'm guessing I have other ways to check for that. – CrimzonWeb Jul 04 '18 at 13:50

1 Answers1

0

I'm currently using this code to check if my app is running. It works perfectly here. Just copy/paste this code:

public static boolean isAppRunning(final Context context) {
    final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    if (procInfos != null) {
        for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
            if (processInfo.processName.equals(BuildConfig.APPLICATION_ID)) {
                return true;
            }
        }
    }
    return false;
}

What I'm doing here? I'm just searching on all active apps process and check if there is a process with the same Name as my app name. Note that the process name is the package name, so it is checking fine. Try it.

Pedro Massango
  • 4,114
  • 2
  • 28
  • 48
  • Wouldn't this solution mean that for example when I open my app, close it and then open a different one it would still count as my app being in foreground cause you are only searching if the app shows up in all active apps process list? – CrimzonWeb Jul 04 '18 at 12:19
  • Yah. Every time that your app is opened, if you call this method, your app will be closed – Pedro Massango Jul 04 '18 at 18:54