Get info about all running processes [closed]

我是研究僧i 提交于 2019-12-12 10:44:32

问题


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


回答1:


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;
    }


来源:https://stackoverflow.com/questions/25721994/get-info-about-all-running-processes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!