I'm using Root Tools and I'm not getting too far with it...
I want to have access to delete files from /system/app and other directories that require root. I would love to do it programmatically through Java, but if I can do it through shell commands, that's OK. Too. My phone is rooted. Here's what I've tried so far.
RootTools.remount("/system", "rw");
try {
RootTools.sendShell("rm /system/app/Videos.apk", -1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RootToolsException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
I've also tried:
public static void sendShell(List<String> cmds) throws Exception {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
}
I have no idea what to do from here...
Based on our conversations it looks like you need a different rom as there were issues remounting partitions on your device.
I would bet that flashing a new rom would alleviate this issue and allow RootTools to properly remount /system as rw and allow you to delete your file.
Related
We know Android OS will select some activities or services to kill if the system needs more resource. I want to run a test to see if my service will be the candidate to be killed. How can I create a situation to trigger the event?
method 1
command kill -9 pid
This is actually a shell command. We know that the bottom layer of Android is Linux. Therefore, all Linux terminal commands can be used on Android. Paste a piece of code to show how do you incorporate it into the code.
private void killProcess(String pid) {
Process sh = null;
DataOutputStream os = null;
try {
sh = Runtime.getRuntime().exec("su");
os = new DataOutputStream(sh.getOutputStream());
final String Command = "kill -9 " + pid + "\n";
os.writeBytes(Command);
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sh.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The most important function of this method is to tell you how to execute Linux shell commands in an Android program.
method 2
Kill the background service without automatic startup:
am (Activity Manager) command
The am command is a command in the /system/bin/ directory of the Android system. You can not only start an application on the terminal, but also start a service, send broadcast, intent action, and force stop process. We're going to use a function that is to force the application to stop.
For the description and usage of the am command, see the Android official website at http://developer.android.com/tools/help/adb.html#am.
The following is an example of code: am force-stop <PACKAGE>
private void forceStopAPK(String pkgName){
Process sh = null;
DataOutputStream os = null;
try {
sh = Runtime.getRuntime().exec("su");
os = new DataOutputStream(sh.getOutputStream());
final String Command = "am force-stop "+pkgName+ "\n";
os.writeBytes(Command);
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sh.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In the preceding code, we call the forceStopAPK method to pass the package name of an application. Then we can kill the corresponding Android application without starting it automatically.
I'm trying to get a file in the assetsbut when I run my code the FileNotFound exception throws. I've checked with getAssets().list(".") and really the android does not detect the file in the assets directory, but i put the file in the assets folder. I'm not a android expert and I count with the community help to help me to understand what am I doing wrong.
Follow bellow my code:
try {
Log.i("PEDRO", Arrays.toString(getResources().getAssets().list(".")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Result: []
try {
Log.i("PEDRO", Arrays.toString(getApplicationContext().getAssets().list(".")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Result: []
The file is in the assets directory.
try {
InputStream input =
Definitions.appContext.getAssets().open("iot.neo..........com.bks");
} catch (IOException e) {
e.printStackTrace();
}
I know that is too much important to use the Android Studio for the development of the applications, but I'm doing it on eclipse because it's a legacy code that will be changed to Android Studio before (and I'm not a responsible for that).
Thanks a lot!
Try remove ".":
try {
Log.d("PEDRO", Arrays.toString(getApplicationContext().getAssets().list("")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
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
I am trying to execute shell command through my code for adding entry in Iptables. The following is my piece of code
private void createShellCommand(String uidValue_param, String interface_param) {
// TODO Auto-generated method stub
StringBuilder cmdScript=new StringBuilder();
script.append("iptables -A OUTPUT " + interface_param + "-m owner --uid-owner " + uidValue_param + "-j REJECT");
writeIptables(cmdScript);
}
private void writeIptables(StringBuilder scriptfile) {
// TODO Auto-generated method stub
String cmd=scriptfile.toString();
if(RootTools.isRootAvailable())
{
Process exec;
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
out.write(cmd);
// Terminating the "su" session
out.write("exit\n");
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("IPtables updation failed", "Iptable write failed"+e);
}
}
else
{
Log.e("Root Access denied", "Access Denied");
}
}
Here there are two methods i.e, createshellCommand() for building the shell command and writeIptables() for updating the iptables. But whenever I run the program logcat is displaying the following warning
"W 19913 su request rejected (0->0 /system/bin/sh)"
But I can manually add the entry through command prompt and its adding to the iptables but by using the above code its not updating. My phone is rooted and I am using android 2.3.4 with linux kernel 2.6.29. And I am using external library "Roottools" for checking the root access.
Please help me to rectify the error.
This Works:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
execute_reboot();
}
void execute_reboot()
{
Process reboot;
try {
reboot=Runtime.getRuntime().exec("su");
DataOutputStream os =
new DataOutputStream(reboot.getOutputStream());
os.writeBytes("reboot\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
reboot.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This Code Works fine. There are couple of small mistakes in your program. Try the one i pasted. Its working charm. All the best. I kept it as simple as possible so that it is easy to understand. You can still use arrays and other stuff to fancy your coding.
And yaa the same one also works for chmod command where you need to pass more than one argument.
For this Just replace
"os.writeBytes("reboot\n");"
with
"chmod 777 /dev/video0\n"(or any other system file).
Thanks. LEt me know if there is something tat i can do.
public static void rebootNow() {
Log.d(TAG, "Rebooting");
try {
#SuppressWarnings("unused")
Process proc = Runtime.getRuntime().exec(
new String[] { "su", "-c", "reboot" });
} catch (Exception ex) {
Log.d(TAG, "Rebooting failed (Terminal Error)");
}
}
This one is a little more compact
You can add "proc.waitFor();" after the Process proc... line to get rid of the unused warning, but rebooting your device takes a few seconds time and if you want to disable some features during the few seconds in your UI-thread, I think its better to not wait for the process to end.
Trying using the iptables command (with sudo and without), rather than just clobbering the iptables config file.
I'm tring to run this:
String[] hin1 = { "su", "-c",
"mount -o remount,rw -t yaffs2 /dev/block/mtdblk3 /system" };
try {
Runtime.getRuntime().exec(hin1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] hin2 = { "su", "-c", "m /system/etc/hosts" };
try {
Runtime.getRuntime().exec(hin2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] hin = { "su", "-c",
"cp /sdcard/hosts /system/etc/" };
try {
Runtime.getRuntime().exec(hin);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Sadly it is only working when I make for every action a new button.. :(
Is there a way to run more than one command at once??
Thanks
Don't think so its working also , I tried the following code :
public class GainrootActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void gainroot(View view)
{
String[] hin1 = { "su", "-c","chmod 777 dev/test1" };
try {
Runtime.getRuntime().exec(hin1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
only button for command su -c chmod 777 dev/test1 (for changing the permission of one log file in dev directory) but it didn't work.
Whats wrong in this.Can some one point out whats missing.
I have even put this line in the Androidmanifest.xml as well
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
Rgds,
Saurabh
Depending on how the su command is implemented (ie, if it's launching something approaching a capable shell as it would on a more typical linux), you may be able to combine multiple commands into one string by separating them with semicolons.
You could also make a shell script containing multiple commands and use su to launch that, though you may need to put it in an executable location.
You're not letting one command finish before the next one starts. Try adding a waitFor after the exec:
Runtime.getRuntime().exec(hin1).waitFor();