1

I am new to Android development, and I try to access to the internal terminal (/bin/bash, ...) of Android phone using a java method.

Do you know if such java method exist?

Thanks

Duke Nukem
  • 319
  • 4
  • 15

1 Answers1

2

You can use Runtime and Process to achieve your task.

private static String executeCommand(String command) {
    Process process = null;
    BufferedReader reader = null;
    String result = "";
    try {
        String line;
        process = Runtime.getRuntime().exec(command);
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        while ((line = reader.readLine()) != null)
            result += line + "\n";
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (process != null)
                process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}

Where command is any available terminal commands like PING 8.8.8.8

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28