2

In an android terminal emulator I can type the following commands:

> su
> echo $(</sys/class/power_supply/battery/charge_rate)

and depending on how the phone is charging the output will be "None", "Normal" or "Turbo". I would like to be able to retrieve this output and store it as a string value in my program.

So I've done a little bit of research into this and the code I came up with is as follows:

    String chargeRate = "None";
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su \"\"echo $(</sys/class/power_supply/battery/charge_rate)");

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

        if ((chargeRate = stdInput.readLine()) == null)
            chargeRate = "None";
    }
    catch (Exception e) {
        // TODO
    }

This is taken from a number of different answers and I'm not quite sure whats wrong with it. I can't step over or past this line while debugging:

if ((chargeRate = stdInput.readLine()) == null)

As soon as the debugger reaches this line it says "The application is running"

Adam Higgins
  • 705
  • 1
  • 10
  • 25
  • 1
    Does this answer your question? [Execute shell commands and get output in a TextView](https://stackoverflow.com/questions/23608005/execute-shell-commands-and-get-output-in-a-textview) – ralf htp Jan 08 '20 at 14:16
  • 1
    updated answer... – ralf htp Jan 08 '20 at 15:56

1 Answers1

1

UPDATE : Solution is in Unable using Runtime.exec() to execute shell command "echo" in Android Java code :

Runtime.getRuntime.exec() doesn't execute a shell command directly, it executes an executable with arguments. "echo" is a builtin shell command. It is actually a part of the argument of the executable sh with the option -c. Commands like ls are actual executables. You can use type echo and type ls command in adb shell to see the difference.

So final code is:

String[] cmdline = { "sh", "-c", "echo $..." }; 
Runtime.getRuntime().exec(cmdline);

cat is also executable from within Runtime.exec() without invoking sh

This is also analyzed in https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html?page=2 in paragraph Assuming a command is an executable program

The code in Execute shell commands and get output in a TextView is good although it uses a command that is executable directly (ls, see update above) :

try {
        // Executes the command.
        Process process = Runtime.getRuntime().exec("ls -l");

        // Reads stdout.
        // NOTE: You can write to stdin of the command using
        //       process.getOutputStream().
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        // Waits for the command to finish.
        process.waitFor();

        return output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • Unfortunately that code gave me the same reaction as the original code, I'll have a look through that question post that you liked. Thanks for the help – Adam Higgins Jan 08 '20 at 14:59
  • 1
    On my phone `echo` does not work, for me works `cat /sys/class/power_supply/battery/batt_temp` even without `su`. It appears that the `echo` command doesnot return in your case, see https://stackoverflow.com/questions/5483830/process-waitfor-never-returns – ralf htp Jan 08 '20 at 15:21
  • 1
    Possibly this is a permissions / security issue (command injection), see https://stackoverflow.com/questions/11268189/security-concerns-with-runtime-exec and https://security.stackexchange.com/questions/152792/java-command-execution-without-runtime-exec and check the permissions of your app – ralf htp Jan 08 '20 at 15:38
  • The link to the question in your updated answer was useful, thanks – Adam Higgins Jan 13 '20 at 11:07
  • For anyone else that ends up here I did need to change the command array to String[] cmdline = { "su", "sh", "-c", "echo $..." }; as i needed SU privileges – Adam Higgins Jan 13 '20 at 11:29