2014-09-23 102 views
2

我是Android编程的新手,我需要创建一个Android应用程序来运行Shell脚本上的按钮单击事件。 我正在使用下面的命令,但没有发生任何操作。Android程序运行Shell脚本

Process p; 
       try { 
        p = new ProcessBuilder() 
        .command("/data/test.sh") 
        .start(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        if(p!=null) p.destroy(); 
       } 
  1. 我已经创建了 “Data” 文件夹,并保持我的shell脚本存在。
  2. 拥有电话根权限。

在此先感谢。 :)

回答

1

要执行shell脚本,您首先需要通过使用chmod +x来为其执行权限。
然后,您可以使用p.waitFor()(在start()之后)并等待它完成执行。

要读你的脚本的输出,你可以做到以下几点:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
// reading part comes here 

如果您在模拟器中运行这个,你需要看看这个:
http://russelldavis.blogspot.in/2011/01/rooting-android-emulator.html

来源:
execute shell command from android
How to Executing Shell commands through android app?

旁白:
Difference between ProcessBuilder and Runtime.exec()

+0

谢谢,请尝试遵循... :) – 2014-09-23 09:37:23