问题
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