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();
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.
code snippet
void takeSnapShot()
{
Process process = null;
try
{
process = Runtime.getRuntime().exec("/system/bin/screencap -p /sdcard/snapshot/test_2.png" );
try
{
process.waitFor();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am gettin an empty file saved whereas If i use the same command through adb shell, i get my screen captured.
Any help will be appreciatable
Edit: my previous answer was mistaken, any app can use the screen capture command.
It might be a permission issue. Are you sure you have permission to write to sdcard?
Check this post, which covers your topic:
How to run android system app without root permisson?
I solved this by adding the following permission to my Manifest.
<uses-permission
android:name="android.permission.READ_FRAME_BUFFER"
tools:ignore="ProtectedPermissions"/>
NOTE: This is a protected permission, my application is a system app and also signed using the platform key.
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.
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
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.