如何在android中以编程方式执行终端命令

时间:2013-12-11 20:02:19

标签: android process terminal command runtime.exec

在我的应用程序中,我想执行Linux-Terminal-Commands,例如“ls”和 在EditText中显示结果。

1 个答案:

答案 0 :(得分:0)

试试这个

    try
    {
        Process process = Runtime.getRuntime().exec("ls");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        StringBuilder result = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null)
        {
            result.append(line + "\n");
        }
        EditText et = (EditText) findViewById(R.id.edittext);
        et.setText(result.toString());
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }