Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从android应用程序运行.sh文件?_Android_Adb_Adb Shell - Fatal编程技术网

如何从android应用程序运行.sh文件?

如何从android应用程序运行.sh文件?,android,adb,adb-shell,Android,Adb,Adb Shell,我的设备已根目录,现在我想从android应用程序运行一个.sh文件。我尝试了以下代码,但没有提供预期的输出: Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh"); 如果我从adb运行.sh文件,它对我来说工作正常。尝试以下代码 Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh"); Scanner stdou

我的设备已根目录,现在我想从android应用程序运行一个
.sh
文件。我尝试了以下代码,但没有提供预期的输出:

Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
如果我从adb运行
.sh
文件,它对我来说工作正常。

尝试以下代码

Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
Scanner stdout = new Scanner(process.getInputStream());
while (stdout.hasNextLine()) {
    Log.i("stdout", stdout.nextLine());
}
stdout.close();
Scanner stderr = new Scanner(process.getErrorStream());
while (stderr.hasNextLine()) {
    Log.e("stderr", stderr.nextLine());
}
stderr.close();
     try{
        Process root = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(root.getOutputStream());
        os.writeBytes("sh /system/bin/xyz.sh \n");
        os.flush();
     }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (SecurityException se){
        se.printStackTrace();
    }

这段代码对我很有用,希望能对您有所帮助。

谢谢您的回复,但这段代码也没用!:-(@Newbie what不起作用?您需要更具体地说,..sh文件没有被执行。语法和拼写编辑,但作者必须添加更多关于预期内容和错误输出的详细信息