8

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

Zaid Iqbal
  • 123
  • 1
  • 5
  • 12
  • 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. – user207421 Sep 08 '14 at 10:24
  • Possible duplicate of this Question is http://stackoverflow.com/questions/3278895/how-to-check-current-running-applications-in-android – Boopathi Sep 08 '14 at 10:30

1 Answers1

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;
    }
Amit Anand
  • 1,225
  • 1
  • 16
  • 40