I need to open USB Mass Storage Activity from my application.
Is there any Intent to do this?
something like
startActivity(new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS))
You can try to use following:
su -c setprop sys.usb.config <command>
Full list of can be found by this one command:
cat init.usb.rc
Function to be able to run command from app:
public void RunAsRoot(String[] cmds){
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
}
Related
I am able to disable the System bar using the following adb command using a terminal client:
adb shell service call activity 42 s16 com.android.systemui
However, I want to achieve this programatically. I looked up how to execute adb commands programatically and found below code:
Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
I replaced "your command" with my adb command above but it does not disable the system bar. How can I achieve this?
You will need root access to do this from an app because ADB can access /system/bin/service but your app cannot (see GID of app and ADB).
Process su = null;
try {
su = Runtime.getRuntime().exec("su");
String cmd = "service call activity 42 s16 com.android.systemui\n";
su.getOutputStream().write(cmd.getBytes());
String exit = "exit\n";
su.getOutputStream().write(exit.getBytes());
su.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (su != null) {
su.destroy();
}
}
You should also check out third party libraries for handling su commands: https://android-arsenal.com/details/1/451
I'm developing an application that execute multiple shell command in different time.
I'm using the following method:
public void RunAsRoot(String[] cmds){
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
}
This method works fine but it always open a new shell every time I need to call it, so that it display the annoying toast "Application has been granted for root permission".
I think this is becouse it always open and close a new shell with SU access. My question is: is there any way to leave a SU shell always open so that I can run my commands when needed without receive the SU toast?
So this might be a bit late, but in case you are still searching for a solution: Just declare
private Process p = Runtime.getRuntime().exec("su");
globally in your class. This should fit your needs.
You can actually spawn the process in onResume() and destroy() it again in onPause().
#Override
onResume() {
if(//check for root) {
try {
this.p = Runtime.getRuntime().exec("su");
}
catch(IOException e) {
// Exception handling goes here
}
}
//set up everything else
}
#Override
onPause() {
this.p.destroy();
}
BTW: I see a serious memory leak in the above method: You open a variety of SU processes but never destroy() them again. Depending on how often you call this method there will be more and more SU processes lying around in RAM until your app is closed.
So
public void runAsRoot(String[] cmds){
try {
Process p = Runtime.getRuntime().exec("su");
}
catch(IOException e) {
// Exception handling goes here
}
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
p.destroy();
}
would be good here. I'm also asking myself if you can compile your approach. Usually Runtime.getRuntime().exec(); has to be surrounded by try/catch.
I want to install .apk silently in background by BusyBox command. I`ve seen some similar questions like THIS, but I still cant get working my code properly...
I have:
My .apk I need to install on /sdcard/download/app.apk
Root
BusyBox installed
Code (not working):
String sss = Environment.getExternalStorageDirectory() + "/download/" + "app.apk";
Process install;
install = Runtime.getRuntime().exec("/system/xbin/busybox pm install " + sss);
int success = install.waitFor();
If I use "install" instead of "pm install" it copies file well.
P.S. Code above is executing in AsyncTask. No errors, but also nothing happens...
Please help!
Also I tried this, but I`m getting exit value 139 and no result:
Process process;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("pm install /mnt/sdcard/app.apk\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
int i = process.waitFor();
maybe this code will help you
Process p = null;
try
{
p = Runtime.getRuntime().exec("su");
DataOutputStream outs=new DataOutputStream(p.getOutputStream());
String cmd="pm install /mnt/sdcard/app.apk";
outs.writeBytes(cmd+"\n");
}
catch (IOException e)
{
e.printStackTrace();
}
After a lot of investigations on many android devices I realized that this code is correct and works!
There was just some problem with one device (Samsung Galaxy Tab 2 7.0 - 4.0.3 ICS). Maybe that is some strange feature of ICS. After updating firmware to 4.1.2 (Jelly Bean) problem has been resolved.
You can simply use adb install command to install/update APK silently. Sample code is below
public static void InstallAPK(String filename){
File file = new File(filename);
if(file.exists()){
try {
String command;
filename = StringUtil.insertEscape(filename);
command = "adb install -r " + filename;
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am trying to run these shell commands via java but no success.Code executes perfectly but .so file do not exectue. while i use these commands in adb everything work perfeclty.
private void submit() {
System.out.println("doooooooooo");
try {
String[] commands = {"cd /data/data/com.dailydeals.usethisnow/lib",
"./libdeals.so" };
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("doneooooooooo");
}
Executing shell commands in Android Applications (Android Programming)
I am trying to run
String command = "su -c 'busybox ls /data'";
p = Runtime.getRuntime().exec(command);
in my app, but it seems like the syntax is somehow wrong. I have no problem running it from the terminal emulator app on the phone, though, so I just can't understand why it is not working when called from within my app.
Any help is deeply appreciated!
SOLUTION FOUND! Thanks to the link suggested by onit here. See the code below: for superuser shell commands to work properly, you first need to create a superuser shell and assign it to a process, then write and read on it's input and output streams respectively.
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
//do something with the output
Use the function below:
public void shellCommandRunAsRoot(String Command)
{
try
{
Process RunProcess= Runtime.getRuntime().exec("su");
DataOutputStream os;
os = new DataOutputStream(RunProcess.getOutputStream());
os.writeBytes(cmds+"\n");
os.writeBytes("exit+\n");
os.flush();
}
catch (IOException e)
{
// Handle Exception
}
}
Usage:
shellCommandRunAsRoot("pkill firefox");