3

I am building an android app in which I need to show the list of currently running apps but It contain all the process including many system or defualt process of android like : launcher,dailer etc.

Now is there any way to check if the currently running process is not a system process (default process) of android.

Thanks a lot.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60

4 Answers4

5

Here is my code: First to get a list of running Apps do the following

public void RunningApps() {
        final PackageManager pm = getPackageManager();
        //Get the Activity Manager Object
        ActivityManager aManager = 
        (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        //Get the list of running Applications
        List<ActivityManager.RunningAppProcessInfo> rapInfoList = 
                          aManager.getRunningAppProcesses();
        //Iterate all running apps to get their details
        for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
            //error getting package name for this process so move on
            if (rapInfo.pkgList.length == 0)
                continue; 

            try {
                PackageInfo pkgInfo = pm.getPackageInfo(rapInfo.pkgList[0],
                PackageManager.GET_ACTIVITIES);

                if (isSystemPackage(pkgInfo)) {
                    //do something here
                } 
                else {
                    //do something here
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                Log.d(TAG, "NameNotFoundException :" + rapInfo.pkgList[0]);
            }
        }
    }

The actual function to check if the running application is system app, and used in the previous method is given below

 /**
     * Return whether the given PackgeInfo represents a system package or not.
     * User-installed packages (Market or otherwise) should not be denoted as
     * system packages.
     *
     * @param pkgInfo The Package info object
     * @return Boolean value indicating if the application is system app
     */
    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return (pkgInfo.applicationInfo.flags & 
                ApplicationInfo.FLAG_SYSTEM) != 0;
    }

I hope it helps.

Khurram Majeed
  • 2,291
  • 8
  • 37
  • 59
  • This is not returning running system process `val rapInfoList = aManager.runningAppProcesses` This returns only app processes. Do you know how to get running system process list? – Kushal Apr 11 '20 at 07:51
3

As per this documentation, it seems you can use FLAG_SYSTEM_PROCESS to identify a process is System process or not. Here is SO discussion on this.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Thanks for the reply. But I think FLAG_SYSTEM_PROCESS is for the RunningService not for RunningProcess . I need to check for Currently RunningProcess. – Dinesh Sharma Jan 24 '12 at 05:21
0
public static boolean isAppRunning(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
    if (list.size() <= 0) {
      return false;
    }
    for (ActivityManager.RunningTaskInfo info : list) {
      if (info.baseActivity.getPackageName().equals(packageName)) {
        return true;
      }
    }
    return false;
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
yu he
  • 1
0

You can use ActivtyManager to get list of all running process in android.See the below link for more information Android process killer .

Community
  • 1
  • 1
himanshu
  • 1,990
  • 3
  • 18
  • 36
  • this is not what I want . I already have the list of running process but I want to check is if any one them is a system process of android ... – Dinesh Sharma Jan 24 '12 at 06:06