I want to run tap cmd like over 100 times and by using code below it will call su everytime and delay taps. So is it possible to request su when app first starts then run many commands fast? thanks!
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
String cmd = "/system/bin/input tap 350 370\n";
os.writeBytes(cmd);
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
}
This is the solution:
try {
Process process = Runtime.getRuntime().exec("su");
OutputStream out = process.getOutputStream();
String cmd = "input tap 350 370";
out.write(cmd.getBytes());
out.flush();
out.close();
process.waitFor();
} catch (IOException e) {
} catch (InterruptedException e) {
}
It worked for me
Related
I'm trying to reboot programmatically my Galaxy S3.
Things that I've tried:
try {
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
proc.waitFor();
} catch (Exception ex) {
Log.i("RebootActivity", "Could not reboot", ex);
}
try {
Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"});
} catch (IOException e) {
e.printStackTrace();
}
try
{
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("reboot now\n");
}
catch (Throwable t)
{
t.printStackTrace();
}
Do you guys have any idea how to accomplish this?
Thanks.
Try to do one normal string looking like 《su \n reboot; \n》 instead of an array.
Try to get the answer from the shell, that helps a lot for debugging.
What are the permissions of your su binary? If they would be wrong, you could try to 《chmod 7777 /system/xbin/su》 after 《mount -o remount,rw /system》
Here is some example code: (to run the string command, which is a \n and or ; separated list of linux shell commands)
StringBuffer commandOutput = new StringBuffer();
try {
Process process = Runtime.getRuntime().exec("su\n");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/vendor/lib:/system/lib\n");
out.writeBytes(command+"\n");
out.flush();
process.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
int numRead;
char[] buffer = new char[1000];
while ((numRead = in.read(buffer)) > 0) {
commandOutput.append(buffer, 0, numRead);
}
in.close();
process.waitFor();
} catch ...
return commandOutput.toString();
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
I tried to uninstall apps on my rooted phone,and I usethe code from How to uninstall Android App with root permissions? ,and I tried the suggestion but I failed.
Here is my code:
Process process;
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("pm uninstall com.lixiancheng.orangemusic"+"; \n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
Why i can't uninstall the app?is any problem with the code?
Have You tried:
try {
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("pm uninstall com.lixiancheng.orangemusic\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
} catch(IOException e){
throw new Exception(e);
} catch(InterruptedException e){
throw new Exception(e);
}
Type adb shell rm -f/{data,system}/app/APKNAME”, replace “APKNAKE” with the name of the application you want to delete and press Enter.
I'm tryng to run rsync on my device. I have the binary in /system/xbin/rsync and I'm trying to launch it with Runtime.getRuntime().exec. I'm new to Android programming, so I don't get yet if I have to user Superuser Permissions.
My code is:
try {
Process process = Runtime.getRuntime().exec(
"/system/xbin/rsync -avzru /sdcard/Tinybox/ " +
"mattiazeni#192.168.1.6::88124bb378ac994088e704d553de6f19");
System.out.print("RSYNC LAUNCHED\n");
// 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();
System.out.print(output);
System.out.print("RSYNC FINISHED\n");
// return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
And it doesn't work, it just prints "RSYNC STARTED" "RSYNC FINISHED".
But if I run:
Process process = Runtime.getRuntime().exec("/system/xbin/rsync --help");
it works fine, I can see the output from the LogCat window.
So I guess I have to use Superuser Permissions and so I modified my code as follows:
try {
System.out.print("RSYNC STARTED\n");
Process process = Runtime.getRuntime().exec("/system/xbin/su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
DataInputStream is = new DataInputStream(process.getInputStream());
os.writeBytes("/system/xbin/rsync --help");
String output = new String();
String temp = new String();
output = is.readLine();
System.out.print(output);
os.flush();
System.out.print("RSYNC FINISHED\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
But when I run the code the App freezes with no errors.
Ok, I'm using a slightly different version of the code:
try {
System.out.print("RSYNC STARTED\n");
process = Runtime.getRuntime().exec("/system/xbin/su -c sh");
OutputStream os = process.getOutputStream();
Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni#192.168.1.6::TinyBox &");
writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni#192.168.1.6::TinyBox &");
os.flush();
System.out.print("RSYNC FINISHED\n");
}
catch ( IOException e ) {
e.printStackTrace();
}
I understood that the problem is on the su command. If I launch rsync using su it blocks with no error message as I previously said, if I remove the su command and launch just:
try {
System.out.print("RSYNC STARTED\n");
process = Runtime.getRuntime().exec("sh");
OutputStream os = process.getOutputStream();
Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni#192.168.1.6::TinyBox &");
writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni#192.168.1.6::TinyBox &");
os.flush();
System.out.print("RSYNC FINISHED\n");
}
catch ( IOException e ) {
e.printStackTrace();
}
it works fine, but of course I got an error from rsync because I have no permissions and the synchronization won't work. How can I solve my problem??
On a rooted android device, I tried to run a cat command that read kernel log, as follow:
Process p = Runtime.getRuntime().exec("su");
p = Runtime.getRuntime().exec("/system/bin/cat /proc/kmsg");
The su command was successfully executed but not the cat.
I tried to read the output of the command using getInputStream() but nothing was there, as follow:
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((read = err.read(buffer)) > 0)
{ //read error to buffer
catOutput.append(buffer, 0, read);
}
in.close();
I used the same code with ls command instead of displaying the kernel log, it worked just fine and show me the result.
I wonder if what error I am getting and wantted to see the error message on the shell when executing the cat command. Tried the p.getErrorStream() but it doesn't give me any result.
Could any one help me with this ? Thanks.
Here's a comprehensive example on how to do this - note that I got the idea from this answer:
public void catKmsg() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
StringBuilder sbstdErr = new StringBuilder();
String command="/system/bin/cat /proc/kmsg";
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) {
}
}
I finally found the solution for the problem by using RootTools library.
Recently released (few months after my question was asked), RootTools provides a easy-to-use tool set that helps running commands that required root privilege. I created a wrapper to check if root access is available before executing shell command:
void testRootToolsCommand(String command){
if (RootTools.isRootAvailable())
toastMessage("Root is available !!!");
else {
toastMessage("NO ROOT !!! ");
return;
}
int timeOut = 1000;
try {
List<String> output = RootTools.sendShell(command,timeOut);
toastMessage("OUTPUT of the command \n" + output.toString());
} catch (RootToolsException re) {
toastMessage("Funny thing happened with RootTools!!! ");
} catch (TimeoutException te)
{
toastMessage("Timeout exception - Increase timeout !!! !!! ");
}
catch (Exception e) {
toastMessage(e.getMessage().toString());
}
}
An example of a function call is:
testRootToolsCommand("cat /proc/kmsg > /sdcard/jun11_4h51.txt");
Note: The Tool also support running multiple commands at once.
I'm trying to create a ServerSocket on a port below 1024.
Obviously, this is restricted to root access only.
But I'm struggling on getting the right permissions for my app.
I'm using this code, for example, to test if I have root access (or trigger the dialog)
But it still doesn't let me ServerSocket work.
AFAIK, the process that is created with the SU command has the root access, not my app. How do I get root access into my own process?
public stat
ic boolean getRoot() {
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
return true;
} else {
return false;
}
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Afaik you cannot open a port below 1024 under *nix systems if you're not root...