I have a weird problem. Last week I had my code working like a charm. I'm working on a rooted tablet, and I'm setting Debug Mode on from code, but no idea why, I ran my app this morning and the command I'm using to to this doesn't work anymore, I get "Permission Denial etc.." BUT, when I type the command from my device's terminal, it works... Any help ?
private void grantPermission(){
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
Log.e("PACKAGE", getPackageName());
os.writeBytes("adb shell" + "\n");
os.flush();
os.writeBytes("pm grant "+getPackageName()+" android.permission.CHANGE_CONFIGURATION" + "\n");
os.flush();
os.writeBytes("pm grant "+getPackageName()+" android.permission.WRITE_SECURE_SETTINGS" + "\n");
os.flush();
} catch (Exception e){
e.printStackTrace();
Log.e("SETTINGS", "FAIL");
}
}
Here it is! I finally fixed it out ! The permission wasn't set fast enough until the call, I simply added a call to the processe's waitFor() method and it worked again. But I still wonder why it used to work before !
private void grantPermission(){
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("pm grant "+this.getPackageName()+" android.permission.WRITE_SECURE_SETTINGS" + "\n");
os.writeBytes("pm grant "+this.getPackageName()+" android.permission.CHANGE_CONFIGURATION" + "\n");
os.writeBytes("exit\n");
os.flush();
suProcess.waitFor();
} catch (Exception e){
e.printStackTrace();
}
}
Related
I have a custom device android 4.3. Problem occurs with some commands, one of an example:
su -c 'pm enable com.android.systemui'
When I run this command over adb it works. However when I run the code programatically using this library it just does not work, no error is shown as well.
Interesting observations:
Shell.SU.available() : false
Shell.SU.isSELinuxEnforcing() : false
Ok so device is rooted. Any reason why you are trying to do that command using that library?
What I am trying to say is why can't you just run the shell command yourself?
runRootCommand method:
static boolean runRootCommand(String command) {
boolean status = true;
DataOutputStream os = null;
try {
Process process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException | InterruptedException e) {
Log.e(TAG, e.toString());
status = false;
} finally {
try {
if (os != null)
os.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
status = false;
}
}
return status;
}
And then call that method like this:
boolean success = runRootCommand("pm enable com.android.systemui");
if(success) {
// command was successful
} else {
// command was NOT successful
}
This will run the command as "su" (superuser).
Hope this helps.
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 writing an app that changes the cpu governor similar to setCpu.
The problem im having is sometimes when writing the governor file (a system file), i get an IOException (bad file number) and it seems to happen randomly.
I'm requesting root in my onCreate method and keeping the process as a field:
private Process suProcess;
public void onCreate(Bundle ...) {
...
suProcess = Runtime.getRuntime().exec("su");
...
and when i need to write a system file this is my code:
try {
DataOutputStream out = new DataOutputStream(suProcess.getOutputStream());
out.writeBytes("echo " + FILE_CONTENTS + " > " + SYSTEM_FILE);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Is this the proper way to handle root permissions? And why am i getting the IOException randomly? (sometimes it works sometimes not)
Not alot of interest in this question apparently but heres my solution.
Call
Runtime.getRuntime().exec("su");
in onCreate to ensure the user doesn't have to deal with the SU dialog during use.
Then when writing system files create another SU process (application should already have su permission)
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(suProcess.getOutputStream());
out.writeBytes("echo " + FILE_CONTENTS + " > " + SYSTEM_FILE);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Please Tell me it is possible to run a shell script file from My Android application.
and read the data from script file.
If it is possible than how to proceed , Please give me some guideline.
You can use this code snippet (from Aaron C)
void execCommandLine(String command)
{
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
try
{
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
}
catch (IOException ex)
{
Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
return;
}
finally
{
if (osw != null)
{
try
{
osw.close();
}
catch (IOException e){}
}
}
try
{
proc.waitFor();
}
catch (InterruptedException e){}
if (proc.exitValue() != 0)
{
Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue());
}
}
But this requires root access I think.
You could also try to use GScript
I've been using this to run shell scripts in my android app. Only thing I've yet to figure out how to do is direct the output to where I want it. You don't need root for this, which is why I'm posting.
Process process = Runtime.getRuntime().exec("top -n 1");
//Get the output of top so that it can be read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
I am trying to write an android application that runs a shell commands, or a shell script if that is preferable, and displays the output... can anyone give me in the right direction?
My code is as follows:
void execCommandLine()
{
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
try
{
String[] str={"/system/bin/sh","/data/shTest.sh"};
System.out.println("EXEC STRING");
proc = runtime.exec(str);
osw = new OutputStreamWriter(proc.getOutputStream());
//osw.write(command);
osw.flush();
osw.close();
}
catch (IOException ex)
{
Log.e("erre","ioexception");
//Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
return;
}
finally
{
if (osw != null)
{
try
{
osw.close();
}
catch (IOException e){}
}
}
try
{
proc.waitFor();
}
catch (InterruptedException e){}
if (proc.exitValue() != 0)
{
Log.e("erre","interruotexception");
//Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue());
}
}
// **************************************
Code is running successfully but I am not getting any output in adb shell logcat
would anyone tell me if this script is executed successfully how to get this output
in Adb shell.
Have you looked into GScript. It is quite flexible.