2

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
  • 329
  • 3
  • 14

2 Answers2

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,076
  • 1
  • 12
  • 27
1

I'm not entirely sure as to what you mean by "accessing the internal terminal". But if you would like to execute commands take a look at the documentation of the Runtime class.

Here's an example on how to use it.

Community
  • 1
  • 1
Kim Visscher
  • 121
  • 1
  • 6
  • I don't want to exec commands on the host OS, but on Android. For example, I want to perform : "ls -l /data/app/*" from a java method to list all APK installed on the phone. – Duke Nukem Apr 20 '17 at 12:11
  • 1
    I misunderstood. That the way I was looking for. Thanks – Duke Nukem Apr 20 '17 at 12:23