使用@Blade0rz 给出的链接中的示例,我对其进行了修改以从内部存储器中获取文件。基本上,我将内容复制到 sdcard 上的测试文件中。随后,我可以用它做任何事情,之后不需要 root 访问权限。
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
String path = Environment.getExternalStorageDirectory().getPath() + "/test.txt";
os.writeBytes("cat /data/test.conf >" + path +"\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
Toast.makeText(getApplicationContext(), path, Toast.LENGTH_LONG).show();
try {
p.waitFor();
if (p.exitValue() != 255) {
// TODO Code to run on success
Toast.makeText(getApplicationContext(), "Phone is Rooted.", Toast.LENGTH_LONG).show();
}
else {
// TODO Code to run on unsuccessful
Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Code to run in input/output exception
Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
}