How to execute terminal commands programmatically in android

294 Views Asked by At

In my App I want to execute Linux-Terminal-Commands e.g. "ls" and show the result in an EditText.

1

There are 1 best solutions below

1
On

Try this

    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);
    }