Runtime.exec() : Reboot in Android?
reboot works fine in android. you are probably not doing runtime.exec() properly.
you need to handle the
public static void rebootSU() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
StringBuilder sbstdErr = new StringBuilder();
String command="/system/bin/reboot";
try { // Run Script
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null)
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
.getInputStream())));
sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
.getErrorStream())));
if (proc.exitValue() != 0) {
}
}
Can't reboot device using runtime.exec
Finally after weeks of searching:
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
Android Runtime.getRuntime().exec command causes app to freeze before getting an output
To avoid UI freezes run in thread
new Thread(new Runnable() {
public void run() {
try {
// Send script into runtime process
Process process = Runtime.getRuntime().exec(pingCommand);
// ......
} catch (IOException e) {
e.printStackTrace();
} finally {
// .....
}
}
}).start();
}
Alternative
You can use AsyncTask
like this:
private class YourTasksClass extends AsyncTask<Void, Void, String> {
private String cmd;
public YourTasksClass(String command) {
this.cmd = command;
}
@Override
protected String doInBackground(Void... voids) {
try {
Process process = Runtime.getRuntime().exec(cmd);
// ....
String someResult = "some kind of result";
return someResult;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null) {
Log.d("tag", "Result: " + result);
}
}
}
Later in your code, you can call this by:
new YourTasksClass(pingCommand).execute();
Android superuser shutdown/reboot commands not doing anything
You can't "su" in one process and "reboot" in another expecting it to have super user status. The command "su -c reboot" might work for you though.
Reboot programmatically Android Things
/system/bin/reboot
binary in DP 4, so as in all the previous dev previews, has world-executable permission, i.e. the following yields
adb shell ls -l /system/bin | grep reboot
-rwxr-xr-x 1 root shell ... reboot
That said, it is yet possible to execute the binary for any user (a.k.a app process in Android) without a need to grab su
. Just execute in Java for
rebooting
Runtime.getRuntime().exec("reboot");
or for powering off
Runtime.getRuntime().exec("reboot -p");
No permission's needed in AndroidManifest.xml
to run the binary successfully.
Caution: in case of security model changes in newer OS versions this approach may not work.
process continues to run after using Java exec
You can call pr.destroy()
after your command execution.
Or you can kill the process via the taskkill
command:
rt.exec("taskkill /F /IM adb.exe")
Runtime.getRuntime().exec(logcat ) prints only 1 line
I have also granted permission
First, permissions are case-sensitive, and therefore your permission is wrong.
Second, only apps installed on the system partition, or apps signed with the firmware's signing key, can hold READ_LOGS
, as of Android 4.1.
Can anyone tell me, how to print whole logcat
Ordinary Android apps cannot do this on modern hardware, for privacy and security reasons, as too many apps log too much crap to LogCat.
Java Runtime.getRuntime(): getting output from executing a command line program
Here is the way to go:
Runtime rt = Runtime.getRuntime();
String[] commands = {"system.exe", "-get t"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
Read the Javadoc for more details here. ProcessBuilder
would be a good choice to use.
Related Topics
Why Comparing Integer with Int Can Throw Nullpointerexception in Java
Boolean[] VS. Bitset: Which Is More Efficient
How to Get the Memory Location of a Object in Java
Reading in from System.In - Java
What Would an Ast (Abstract Syntax Tree) for an Object-Oriented Programming Language Look Like
Leaking This in Constructor Warning
How to Find How Much Disk Space Is Left Using Java
Why We Can't Do List<Parent> Mylist = Arraylist<Child>();
Environment Variables in Eclipse
Why Does a Try/Catch Block Create New Variable Scope
Java: Literal Percent Sign in Printf Statement
Check Chains of "Get" Calls for Null
How to Match Nested Brackets with a Regex Without Using Recursion or Balancing Groups
How to Create My Own Appender in Log4J