I wanted to know if a process in Android belongs to an app which the user is actively controlling vs if it is a background service which runs without user interaction?
Since I'm talking about process, I'm currently trying to get the data from the List of processes in ActivityManagerService.java. The mLruProcesses contains list of ProcessRecords. So is the flag ProcessRecord.setIsForeground the right one to determine the behaviour of the process (i.e, Foreground vs background)? Or is there a different place to get this information.
Thanks
So after digging through I found out that isForeground is not the right place to check this. Instead we could track the topActivity in the stack to correlate it with the process. If they match then the process belongs to the foreground app if not it is a background.
This post and this post also re-assure the same. I'm posting the same code from the topic for reference.
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
// The first in the list of RunningTasks is the foreground task.
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo.topActivity.getPackageName();
Log.i(TAG, "Printing the Top FG activity: " + foregroundTaskPackageName);
PackageManager pm = mContext.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
Log.i(TAG, "Printing the Top FG App Label: " + foregroundTaskAppName);