Find if a process is foreground active app vs a background service?

713 views
Skip to first unread message

Narasimha Aditya Gollapudi

unread,
Aug 22, 2014, 3:01:37 PM8/22/14
to android-...@googlegroups.com
Hi 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,
Narasimha

Endy Silveira

unread,
Aug 25, 2014, 9:07:37 AM8/25/14
to android-...@googlegroups.com
Hi Narasimha,

I don't have all the answers for your question, but I believe someone have. What I can say, in the way that I use my applications, is that I believe that a "isForeground" will not give you what you want...

I use an application to guarantee that my main application will run 24/7, and this "supervisor" application is not on the Foreground and isn't an Service too...

Best regards,
Endy

Durgadoss Ramanathan

unread,
Aug 25, 2014, 1:05:44 PM8/25/14
to android-...@googlegroups.com
Hi Narasimha,

I believe you could do something like this:

I wrote the the below snippet as a Hack to kill the activity that is currently running (except launcher) .. ;)
--

+               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);
+
+               List<ActivityManager.RunningAppProcessInfo> listOfProcesses = am.getRunningAppProcesses();
+               for (int i = 0; i < listOfProcesses.size(); i++) {
+                       ActivityManager.RunningAppProcessInfo proc = listOfProcesses.get(i);
+                       //don't kill the launcher..It will keep restarting(launcher is the home screen App)
+                       if (proc.processName.equalsIgnoreCase("com.android.launcher"))
+                               continue;
+                       if(proc.processName.equalsIgnoreCase(foregroundTaskPackageName)) {
+                               curFGTaskPid = proc.pid;
+                               Log.i(TAG, "Printing the Top FG activity's PID: " + curFGTaskPid);
+                               break;
+                       }
+               }
+
+               android.os.Process.killProcess(curFGTaskPid);

Thanks,
Durga



--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.



--
Regards
Durgadoss

Narasimha Aditya Gollapudi

unread,
Aug 25, 2014, 3:50:20 PM8/25/14
to android-...@googlegroups.com
Hi Durgadoss,

It seems get the active processes from the current activity in stack seems to be the right way to see if an process is related to a foreground or background. Eventually I implemented something similar too. Thanks for the your reply it is reassuring my approach.

Thanks,
Narasimha

Narasimha Aditya Gollapudi

unread,
Aug 25, 2014, 3:54:17 PM8/25/14
to android-...@googlegroups.com
Hello Endy,

Yes what you have said is right "isForeground" is not the right way. But although the service is running in the background we could correlate it with the topActivity in the stack to see if the service is related to the foreground application. If not then it would be a background task. 

Durgadoss has provided an example snippet of the same which helps.

Thanks,
Narasimha
Reply all
Reply to author
Forward
0 new messages