I would like to run NanoHttpd on an Android (KitKat 4.4) device and bind it to port 80.
I understand that I can only bind to port 80 as a root user. My device has been rooted and I can fire up a process with superuser privileges successfully, like so:
Process p = Runtime.getRuntime().exec("su");
This blog post shows me how to write a file, by using the getOutputStream()
method of Process
.
However, I would like to run NanoHttpd within the rooted process. Is that possible? Or am I restricted to shell commands? Or can I start my app from a script?
You can start your app in a script by using the following command (with superuser rights):
am start -n your.package.name/.YourActivity
Replace your.package.name
and .YourActivity
accordingly.
To use root shell in java:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream doutps = new DataOutputStream(p.getOutputStream());
doutps.writeBytes("yourcommand" + "\n");
doutps.flush();
doutps.writeBytes("exit\n");
doutps.flush();
p.WaitFor();
doutps.close();
try { p.destroy(); } catch(Exception ex) {}
Again, replace yourcommand
with your desired command.
You can use any command that you can use in the root shell, no matter what the command is and how it is constructed. If you need multiple commands, just repeat following lines for every command:
doutps.writeBytes("yourcommand" + "\n");
doutps.flush();