Android程序中执行adb命令

2016-03-15  本文已影响7148人  IT枫

查看程序的调度状态为"bg"的进程信息

PC终端执行如下命令

$adb shell ps|grep bg

程序中如何执行adb命令?

Runtime.getRuntime().exec("ps")
读取命令执行后返回的数据

BufferedReader reader = null;
String content = "";
try {
    //("ps -P|grep bg")执行失败,PC端adb shell ps -P|grep bg执行成功
    //Process process = Runtime.getRuntime().exec("ps -P|grep tv");
    //-P 显示程序调度状态,通常是bg或fg,获取失败返回un和er
    // Process process = Runtime.getRuntime().exec("ps -P");
    //打印进程信息,不过滤任何条件
    Process process = Runtime.getRuntime().exec("ps");
    reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    StringBuffer output = new StringBuffer();
    int read;
    char[] buffer = new char[4096];
    while ((read = reader.read(buffer)) > 0) {
        output.append(buffer, 0, read);
    }
    reader.close();
    content = output.toString();
} catch (IOException e) {
    e.printStackTrace();
}

Android进程的五个级别,对应五个调度状态

1.foreground process
2.visible process
3.Service process
4.background process
5.empty process
打印的结果:读取到部分进程信息,包含系统应用及用户安装的应用,不能读取全部的进程信息

ps命令读取到的进程信息
各字段的含义:
进程信息各字段含义
android adb 查看进程信息
上一篇 下一篇

猜你喜欢

热点阅读