繁体   English   中英

Android Runtime.getRuntime()。exec和rsync

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM