我有以下代码,用于从其 PID 获取应用程序名称。
shell命令是(只是为了显示它在shell提示符下工作,我修剪了很多行,它们看起来都很相似)
C:\android-sdk-windows\platform-tools>adb shell ps
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 312 220 c009b74c 0000ca4c S /init
root 2 0 0 0 c004e72c 00000000 S kthreadd
root 3 2 0 0 c003fdc8 00000000 S ksoftirqd/0
root 4 2 0 0 c004b2c4 00000000 S events/0
root 27 1 740 316 c0158eb0 afd0d8ac S /system/bin/sh
system 28 1 812 292 c01a94a4 afd0db4c S /system/bin/servicemanager
root 29 1 3736 568 ffffffff afd0e1bc S /system/bin/vold
system 68 33 191096 38680 ffffffff afd0db4c S system_server
radio 131 33 147680 22868 ffffffff afd0eb08 S com.android.phone
app_25 134 33 148784 25740 ffffffff afd0eb08 S com.android.launcher
app_0 160 33 135560 20956 ffffffff afd0eb08 S android.process.acore
root 316 40 740 332 c003da38 afd0e7bc S /system/bin/sh
root 317 316 888 336 00000000 afd0d8ac R ps
我的Java代码是:
private static Pattern pidPattern = Pattern.compile("(\\d+).+?\\s+[DNRSTZ]\\s+(.+)$");
private final Map<Integer, String> pidNames = new HashMap<Integer, String>();
private void getAppNames() {
// Bad command: String commandLine = "adb shell ps";
String line;
try {
Process process = Runtime.getRuntime().exec("ps");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Matcher matcher;
pidNames.clear();
while((line = bufferedReader.readLine()) != null) {
matcher = pidPattern.matcher(line);
if(matcher.find()) {
int pid = Integer.valueOf(matcher.group(1));
String name = matcher.group(2);
pidNames.put(pid, name);
Log.i(TAG, "Pid=" + pid + " Name=" + name);
}
}
bufferedReader.close();
process.destroy();
}
catch (IOException e) {
Log.e(TAG, "getAppNames failed", e);
}
}
我收到以下消息:
05-14 17:09:00.220: E/BackTask(698): getAppNames failed
05-14 17:09:00.220: E/BackTask(698): java.io.IOException: Error running exec(). Command: [adb shell ps, android.com] Working Directory: null Environment: [ANDROID_SOCKET_zygote=10, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, PATH=/sbin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar, ANDROID_PROPERTY_WORKSPACE=9,32768, ANDROID_ROOT=/system, LD_LIBRARY_PATH=/system/lib]
05-14 17:09:00.220: E/BackTask(698): at java.lang.ProcessManager.exec(ProcessManager.java:226)
05-14 17:09:00.220: E/BackTask(698): at java.lang.ProcessBuilder.start(ProcessBuilder.java:201)
05-14 17:09:00.220: E/BackTask(698): at com.delta.app.BackTask.getAppNames(BackTask.java:180)
05-14 17:09:00.220: E/BackTask(698): at com.delta.app.BackTask.doInBackground(BackTask.java:65)
05-14 17:09:00.220: E/BackTask(698): at com.delta.app.BackTask.doInBackground(BackTask.java:1)
05-14 17:09:00.220: E/BackTask(698): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-14 17:09:00.220: E/BackTask(698): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-14 17:09:00.220: E/BackTask(698): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-14 17:09:00.220: E/BackTask(698): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-14 17:09:00.220: E/BackTask(698): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-14 17:09:00.220: E/BackTask(698): at java.lang.Thread.run(Thread.java:1096)
05-14 17:09:00.220: E/BackTask(698): Caused by: java.io.IOException: Permission denied
05-14 17:09:00.220: E/BackTask(698): at java.lang.ProcessManager.exec(Native Method)
05-14 17:09:00.220: E/BackTask(698): at java.lang.ProcessManager.exec(ProcessManager.java:224)
05-14 17:09:00.220: E/BackTask(698): ... 10 more
问题是:怎么了?没有权限?进程调用自己?
编辑:尝试了这条线:
Process process = Runtime.getRuntime().exec(commandLine);
具有相同的 IOException 结果