Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to check if a running process is a system process in android

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.

like image 249
Dinesh Sharma Avatar asked Nov 26 '25 19:11

Dinesh Sharma


2 Answers

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.

like image 160
Khurram Majeed Avatar answered Nov 29 '25 09:11

Khurram Majeed


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.

like image 20
kosa Avatar answered Nov 29 '25 10:11

kosa