很多时候对Android系统底层进行操作(如查看/data/data/下的数据)是没有权限的。当然如果能在Java层直接执行的操作,直接用类似如下代码执行即可:
Process process;
String cmd = "....";
try {
process = Runtime.getRuntime().exec("su");
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
process.getOutputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
writer.write(cmd + "\n");
writer.write("exit\n");
writer.flush();
String line;
while ((line = reader.readLine()) != null) {
// 处理结果
}
while ((line = error.readLine()) != null) {
Log.e("error", line);
}
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}