[英]Android Runtime.getRuntime().exec and rsync
我試着在我的設備上運行rsync。 我在/system/xbin/rsync
有二進制文件,我正在嘗試使用Runtime.getRuntime().exec
啟動它。 我是Android編程的新手,所以如果我必須使用超級用戶權限,我還沒有得到。
我的代碼是:
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窗口的輸出。
所以我想我必須使用超級用戶權限,所以我修改了我的代碼如下:
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得到一個錯誤,因為我沒有權限,同步將無法正常工作。 我怎樣才能解決我的問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.