Run android program as root - android

Is it possible to run my program as root? I know how to run command-line native utils, but how to run Java program as root?

This will work for you:
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
You could use this for a command:
exec(new String[] { "su", "-c", COMMAND });
Best wishes,
Tim

Related

How to run .sh file from android app?

My device has been rooted and now i want to run an .sh file from my android application. I tried with following code but it did't provide the intended output:
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
If i run .sh file from adb it is working fine for me.
Try following code.
try{
Process root = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(root.getOutputStream());
os.writeBytes("sh /system/bin/xyz.sh \n");
os.flush();
}
catch (IOException e) {
e.printStackTrace();
}
catch (SecurityException se){
se.printStackTrace();
}
This snippet worked for me,I hope this may help you.
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
Scanner stdout = new Scanner(process.getInputStream());
while (stdout.hasNextLine()) {
Log.i("stdout", stdout.nextLine());
}
stdout.close();
Scanner stderr = new Scanner(process.getErrorStream());
while (stderr.hasNextLine()) {
Log.e("stderr", stderr.nextLine());
}
stderr.close();

Modifying other app's database with root in Android

How can I read and modify the database of an app that I don't own with root.
I tried copying the file to my app's location like this:
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("su -c cat /data/user/0/com.maxmpz.audioplayer/databases/folders.db > /data/user/0/com.example.me.myapp/folders.db");
} catch (IOException e) {
e.printStackTrace();
}
and:
try {
Runtime.getRuntime().exec("su");
Process p = Runtime.getRuntime().exec("su cp /data/user/0/com.maxmpz.audioplayer/databases/folders.db /data/user/0/com.example.me.myapp/folders.db");
} catch (IOException e) {
e.printStackTrace();
}
But nothing happens (the exception isn't raised).

application that execute command in /system

How to create an application that runs a command in /system.
The command that I need to give is: udpxy -p 4022.
This command will start a program that I have in /system.
I do not know which commands use in creating application, which execute this command.
If anyone can help I'll be very grateful.
try {
Process process = Runtime.getRuntime().exec("/system/udpxy -p 4022");
process.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
http://developer.android.com/reference/android/os/Process.html

How to Shutdown Android 4.0 rooted Device programatically

Please help me to find out solution for this, I Know there are so many questions and duplicates about this same but here i describe whole things which i tried.
I have one android device where its installed 4.0 version of android.
I want to shutdown this device using my one demo application.
1) Demo application is signed by platform keys which are used in built in file system.
2) Device is already rooted as its development board and i have all permissions on this.
Application contains Following things
1) Application is system application
2) Application signed by platform keys which are used in built in file system.
For make automation easier, I did import the key/cert pair into my java keystore file, with the this keytool-importkeypair and use eclipse for signing.
Used command is mentioned below.
Commad : keytool-importkeypair -k ~/Desktop/myown.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform
I used following code for reboot but i never make success in this .I read So many questions and answers on stackoverflow but they all said you require
1) root access of device
2) signed apk with any one keys which are available on `build/target/product/security/`
3) Given Proper permission in AndroidManifest.xml file.
Am i right in alomg points?
Application code :
First Try
public static void shutdown2() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
String command = "/system/bin/reboot -p";
try { // Run Script
proc = runtime.exec("/system/xbin/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();
}
if (proc.exitValue() != 0) {
}
}
Second Try :
private void shutdown3() {
try {
String[] cmd = { "/system/bin/sh","su","reboot -p"};
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 3 ", ex);
}
}
3rd Try :
private void shutdown() {
try {
Process proc = Runtime.getRuntime().exec(
new String[] { "/system/bin/su", "-c",
"/system/bin/reboot -p" });
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 1 ", ex);
}
}
In 3rd method I also tried with "/system/bin/su"
The below code worked for me
Process process = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "busybox poweroff -f"});
process.waitFor();
A much better solution is to run:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
You can use a Process for this.

Android 2.2: Reboot device programmatically

I would like to know if there is a way to reboot the device through code. Ive tried:
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
And added permissions for REBOOT but it still doesnt work.
Thanks
This seemed to work for me:
try {
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
proc.waitFor();
} catch (Exception ex) {
Log.i(TAG, "Could not reboot", ex);
}
Still for rooted devices, but in case you want safer (process.waitFor() is conditioned, in separate try-catch, we have proper exception handling, "now" added in command after reboot, which is necessary for some devices, etc.) and maybe cleaner code, take a look at this:
Process rebootProcess = null;
try
{
rebootProcess = Runtime.getRuntime().exec("su -c reboot now");
}
catch (IOException e)
{
// Handle I/O exception.
}
// We waitFor only if we've got the process.
if (rebootProcess != null)
{
try
{
rebootProcess.waitFor();
}
catch (InterruptedException e)
{
// Now handle this exception.
}
}
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
links
link #2
I am using Xamarin. For me the solution is:
Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
Here is a solution. Remember, the device must be rooted.
try{
Process p = Runtime.getRuntime().exec("su");
OutputStream os = p.getOutputStream();
os.write("reboot\n\r".getBytes());
os.flush();
}catch(IOException )
If the phone is rooted, it's actually very simple:
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
The first command will ask for superuser permission. The second, will reboot the phone.
There is no need for extra permissions in the manifest file since the actual rebooting is handled by the executed comamand, not the app.

Categories

Resources