gpt4 book ai didi

Android Runtime.getRuntime().exec 和 rsync

转载 作者:太空狗 更新时间:2023-10-29 15:13:55 25 4
gpt4 key购买 nike

我正在尝试在我的设备上运行 rsync。我在 /system/xbin/rsync 中有二进制文件,我正在尝试使用 Runtime.getRuntime().exec 启动它。我是 Android 编程的新手,所以我还不知道是否必须使用 super 用户权限。

我的代码是:

try {
Process process = Runtime.getRuntime().exec(
"/system/xbin/rsync -avzru /sdcard/Tinybox/ " +
"mattiazeni@192.168.1.6::88124bb378ac994088e704d553de6f19");
System.out.print("RSYNC LAUNCHED\n");

// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();

// Waits for the command to finish.
process.waitFor();
System.out.print(output);
System.out.print("RSYNC FINISHED\n");

// return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

它不起作用,它只是打印“RSYNC STARTED”“RSYNC FINISHED”。

但是如果我运行:

Process process = Runtime.getRuntime().exec("/system/xbin/rsync --help");

它工作正常,我可以看到 LogCat 窗口的输出。

所以我想我必须使用 super 用户权限,所以我修改了我的代码如下:

try {
System.out.print("RSYNC STARTED\n");
Process process = Runtime.getRuntime().exec("/system/xbin/su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
DataInputStream is = new DataInputStream(process.getInputStream());
os.writeBytes("/system/xbin/rsync --help");
String output = new String();
String temp = new String();
output = is.readLine();

System.out.print(output);
os.flush();
System.out.print("RSYNC FINISHED\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}

但是当我运行代码时,应用程序没有错误地卡住。

最佳答案

好的,我使用的代码版本略有不同:

try {
System.out.print("RSYNC STARTED\n");
process = Runtime.getRuntime().exec("/system/xbin/su -c sh");
OutputStream os = process.getOutputStream();
Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");
writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");

os.flush();
System.out.print("RSYNC FINISHED\n");
}
catch ( IOException e ) {
e.printStackTrace();
}

我知道问题出在 su 命令上。如果我使用 su 启动 rsync,它会像我之前所说的那样阻塞而不会出现错误消息,如果我删除 su 命令并仅启动:

try {
System.out.print("RSYNC STARTED\n");
process = Runtime.getRuntime().exec("sh");
OutputStream os = process.getOutputStream();
Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");
writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");

os.flush();
System.out.print("RSYNC FINISHED\n");
}
catch ( IOException e ) {
e.printStackTrace();
}

它工作正常,但当然我从 rsync 收到错误,因为我没有权限并且同步将无法工作。我该如何解决我的问题??

关于Android Runtime.getRuntime().exec 和 rsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com