Reputation: 419
I am creating an Android application that should be able to access the PID's of other applications running on the device (like Google Maps, Calculator, etc.). I have the following code running every second to pull the running processes on the Android device. However, when I run it, it is only showing me info regarding my application, even though 3 other applications are also running. Does anyone have any suggestions on how to get the PID of other applications running on an Android device?
try {
Process process = Runtime.getRuntime().exec("ps");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
while(true) {
String line = in.readLine();
System.out.println(line);
if ( line == null ) break;
}
}
catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 1790
Reputation: 6673
You can get it using Activity Manager
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for (int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
System.out.println(info.pid);
}
Upvotes: 1