2

I have the following method which does not work accurately for the Lollipop code segment. The getRunningAppProcesses() method returns all sorts of system tasks and also returns multiple results which I am unable to determine which result represents the equivalent of using getRunningTasks(1).get(0).topActivity.

How is this suppose to be done for API 21 and above?

   public static boolean isAppInBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            if (!tasks.isEmpty()) {
                for (ActivityManager.RunningAppProcessInfo fullTaskList : tasks) {
                    if (fullTaskList.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                        // NOTE: this condition needs to be changed because
                        // the list returns multiple results and not all 
                        // the results represent IMPORTANCE_BACKGROUND
                        return true;
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (!topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
        }
        return false;
    }
RP-
  • 5,827
  • 2
  • 27
  • 46
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • 1
    This may be some help;http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l – RP- Sep 03 '15 at 01:02
  • possible duplicate of [Checking if an Android application is running in the background](http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – cybersam Sep 03 '15 at 01:38
  • @cybersam this isn't a duplicate thread as I am asking about a way of determining this through a function, whereas that thread is explaining a solution using singleton pattern and android lifecycle. I want to know specifically, how to properly use getRunningAppProcessInfo(). – portfoliobuilder Sep 03 '15 at 16:42

1 Answers1

1

The most simple and straightforward solution that I found is the following:

public class AppStatusHelper {
    private static final String TAG = AppStatusHelper.class.getSimpleName();

    private static int mNumOfActivitiesInOnStarttoOnStopLifeCycle=0;

    public static void onStart()
    {
        mNumOfActivitiesInOnStarttoOnStopLifeCycle++;
    }
    public static void onStop()
    {
        mNumOfActivitiesInOnStarttoOnStopLifeCycle--;
    }

    public static boolean isAppInBackground()
    {
        Log.d(TAG,"num->"+mNumOfActivitiesInOnStarttoOnStopLifeCycle+"\tisOnBackground->"+(mNumOfActivitiesInOnStarttoOnStopLifeCycle==0));
        return mNumOfActivitiesInOnStarttoOnStopLifeCycle==0;
    }

}

Just call onStart on every activity start and onStop on every activity stop. Then check if the counter is equal to 0.

Ozzz
  • 342
  • 5
  • 12