Get info about all running processes [closed]
Asked Answered
C

1

9

I have an idea to make Task Manager for android.Can anyone tell me how to get all the processes currently running in android?

Corvin answered 8/9, 2014 at 10:17 Comment(2)
You can't. You can get all the running processes, by reading the output of ps, but you can't manage them, except by exec-ing other programs, which already exist.Alfrediaalfredo
Possible duplicate of this Question is #3279395Harner
R
12

using the code below you can get the list of running processes:-

ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

for(RunningAppProcessInfo runningProInfo:procInfos){

        Log.d("Running Processes", "()()"+runningProInfo.processName);
}

For more information you can visit this link.

To get the application name based on package name use PackageManager class.

final PackageManager pkgmgr = getApplicationContext().getPackageManager();
ApplicationInfo appinfo;
try {
    appinfo = pkgmgr.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
    appinfo = null;
}
final String applicationName = (String) (appinfo != null ? pkgmgr.getApplicationLabel(appinfo) : "(unknown)");

To get the app name on the basis of PID use:-

public static String getAppNameByPID(Context context, int pid){
    ActivityManager manager 
               = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for(RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()){
        if(processInfo.pid == pid){
            return processInfo.processName;
        }
    }
    return "";
}

and finally to check if an app is system app or not use:-

private boolean isSystemPackage(PackageInfo pkgInfo) {
        return (pkgInfo.applicationInfo.flags & 
                ApplicationInfo.FLAG_SYSTEM) != 0;
    }
Rafa answered 8/9, 2014 at 10:26 Comment(15)
any permissions to mention in menifest?Corvin
@zaidiqbal android.permission.GET_TASKSRafa
It also returns a lot of system processes?Corvin
Also it is returning package names not actual app name.Any solution?Corvin
@zaidiqbal then please checkout this answerRafa
How to get app name through package name?Corvin
Now last thing . how can i get pid through package name?Corvin
@zaidiqbal Check the last part of my edited answer.Rafa
manager.getRunningAppProcesses() gives a lot of tasks While manager.getRecentTasks() gives some tasks. Why? What is difference b/w them?Corvin
getRunningAppProcesses() returns a list of application processes that are running on the device while getRecentTasks(int maxNum, int flags) returns a list of the tasks that the user has recently launched, with the most recent being first and older ones after in order. Here maxNumis the maximum number of entries to return in the list. The actual number returned may be smaller, depending on how many tasks the user has started and the maximum number the system can remember.Rafa
edited my answer. hope I helped :)Rafa
Note that from API 29, a regular app cannot access the list of other processes.Pompei
@is there any solution for that?Morava
@AlexCohn Is there a reference for that? (that a regular app cannot access the list of other processes from API 29?) I'm thinking maybe to post a new question stackoverflow related to that..Derward
@Derward you can check this thread. Unfortunately, as Michael points out, we don't know why Google haven't updated the documentation to reflect this.Pompei

© 2022 - 2024 — McMap. All rights reserved.