Created
December 27, 2023 18:39
-
-
Save cooliscool/b4741292c17b859334642f586c6d6b59 to your computer and use it in GitHub Desktop.
Android Shell Command executor class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.moo.myapplication2; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class CommandExecutor { | |
public static String executeCommand(String command) { | |
StringBuilder output = new StringBuilder(); | |
try { | |
// Execute the command | |
Process process = Runtime.getRuntime().exec(command); | |
// Read the output of the command | |
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
output.append(line).append("\n"); | |
} | |
BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream())); | |
String errLine; | |
while ((errLine = stderr.readLine()) != null) { | |
output.append(errLine).append("\n"); | |
} | |
// Wait for the command to complete | |
int exitCode = process.waitFor(); | |
// Log the exit code | |
output.append("\nExit code: ").append(exitCode); | |
} catch (IOException | InterruptedException e) { | |
e.printStackTrace(); | |
output.append("Error executing command: ").append(e.getMessage()); | |
} | |
return output.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment