如何在Android设备上运行Linux命令?

Wen*_*hen 9 shell android adb

在某些Android设备,在亚洲开发银行的外壳,我只能跑echo,cd,ls.当我跑:

tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Run Code Online (Sandbox Code Playgroud)

或者命令cp,它返回:

sh: tar: not found
Run Code Online (Sandbox Code Playgroud)

为什么我不能运行这些命令?某些设备支持这些命令.我的最终目标是将文件从/ data/data文件夹复制到SD卡.我得到了su,我得到了以下代码:

int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
        + packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
        process.getInputStream())), 64);

String inLine;
try {
    StringBuilder sbCommand = new StringBuilder();
    sbCommand.append(command).append(" ");
    sbCommand.append("\n");
    os.writeBytes(command.toString());
    if (is != null) {
        for (int i = 0; i < timeout; i++) {
            if (is.ready())
                break;
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
        } else {
        }
    }

} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

它总是停在里面is.ready(),当我改变它时process.waitfor()它也停了下来.为什么?

waq*_*lam 10

据我所知,运行shell命令的唯一方法是:

Process proc = Runtime.getRuntime().exec("your command");
Run Code Online (Sandbox Code Playgroud)


key*_*fer 10

您可以在Android上运行Linux命令.但通常只有很少的预安装.如果要添加更多命令,可能需要对设备进行root操作并在其上安装busybox.

这不适用于应用程序中的高效使用,但可以帮助您使用您的设备.