我正在尝试使用以下内容在Android中执行需要root访问权限的命令:
public static void runCmd(String cmd) {
DataOutputStream os;
try {
Process process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
//os.writeBytes(cmd + "\n");
os.writeBytes("echo hello\n"); <---- This fails?
os.writeBytes("exit\n");
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
正如你所看到的,我已经注释掉了实际的命令,以排除它与root访问有关(“echo hello”不需要root权限吗?)。我在Android 4.2设备上使用了很多代码,现在我们必须使用Android 4.4,它不再适用了。我收到错误:
07-19 22:29:29.555 20744-20744/? W/System.err: java.io.IOException: write failed: EPIPE (Broken pipe)
07-19 22:29:29.555 20744-20744/? W/System.err: at libcore.io.IoBridge.write(IoBridge.java:455)
07-19 22:29:29.555 20744-20744/? W/System.err: at java.io.FileOutputStream.write(FileOutputStream.java:187)
07-19 22:29:29.555 20744-20744/? W/System.err: at java.io.OutputStream.write(OutputStream.java:82)
07-19 22:29:29.555 20744-20744/? W/System.err: at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
07-19 22:29:29.555 20744-20744/? W/System.err: at com.company.kioskapp.MainActivity.runCmd(MainActivity.java:298)
07-19 22:29:29.555 20744-20744/? W/System.err: at com.benefittechnologies.benekiosk.MainActivity.disableAndroidUI(MainActivity.java:273)
07-19 22:29:29.555 20744-20744/? W/System.err: at com.benefittechnologies.benekiosk.MainActivity.onCreate(MainActivity.java:124)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.Activity.performCreate(Activity.java:5254)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.os.Looper.loop(Looper.java:136)
07-19 22:29:29.555 20744-20744/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
07-19 22:29:29.555 20744-20744/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-19 22:29:29.555 20744-20744/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
07-19 22:29:29.555 20744-20744/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
07-19 22:29:29.555 20744-20744/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err: Caused by: libcore.io.ErrnoException: write failed: EPIPE (Broken pipe)
07-19 22:29:29.555 20744-20744/? W/System.err: at libcore.io.Posix.writeBytes(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err: at libcore.io.Posix.write(Posix.java:202)
07-19 22:29:29.555 20744-20744/? W/System.err: at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
07-19 22:29:29.555 20744-20744/? W/System.err: at libcore.io.IoBridge.write(IoBridge.java:450)
07-19 22:29:29.555 20744-20744/? W/System.err: ... 20 more
如果我注释掉
os.writeBytes(“echo hello \ n”);
也行,没有例外。