Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,739,676 Members 51,434 Now Online
XDA Developers Android and Mobile Development Forum

Writing in the private area of other apps

Tip us?
 
Masrepus
Old
#11  
Masrepus's Avatar
Senior Member
Thanks Meter 69
Posts: 635
Join Date: Feb 2013
Ah i think i have read about executing an array of commands somewhere, so i guess this will work
Ya probably that is a more elegant solution

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 
arpitkh96
Old
#12  
arpitkh96's Avatar
Senior Member
Thanks Meter 375
Posts: 1,085
Join Date: Feb 2012
Location: panipat
I use this class to execute su commands(extracted from aokp rom control).
Code:
Select Code
 public class CMDProcessor {

private final String TAG = getClass().getSimpleName(); 
private static final boolean DEBUG = false; 
private Boolean can_su; 
public SH sh; 
public SH su;

public CMDProcessor()
{ sh = new SH("sh"); su = new SH("su"); }

public SH suOrSH() { return canSU() ? su : sh; }

public boolean canSU() { return canSU(false); }

public class CommandResult { private final String resultTag = TAG + '.' + getClass().getSimpleName(); public final String stdout; public final String stderr; public final Integer exit_value;

CommandResult(final Integer exit_value_in) { this(exit_value_in, null, null); }

CommandResult(final Integer exit_value_in, final String stdout_in, final String stderr_in) { exit_value = exit_value_in; stdout = stdout_in; stderr = stderr_in; if (DEBUG) Log.d(TAG, resultTag + "( exit_value=" + exit_value + ", stdout=" + stdout + ", stderr=" + stderr + " )"); }

public boolean success() { return exit_value != null && exit_value == 0; }

}

public class SH { private String SHELL = "sh";

public SH(final String SHELL_in) { SHELL = SHELL_in; }

private String getStreamLines(final InputStream is) { String out = null; StringBuffer buffer = null; final DataInputStream dis = new DataInputStream(is); try { if (dis.available() > 0) { buffer = new StringBuffer(dis.readLine()); while (dis.available() > 0) { buffer.append("\n").append(dis.readLine()); } } dis.close(); } catch (final Exception ex) { Log.e(TAG, ex.getMessage()); } if (buffer != null) { out = buffer.toString(); } return out; }

public Process run(final String s) { Process process = null; try { process = Runtime.getRuntime().exec(SHELL); final DataOutputStream toProcess = new DataOutputStream( process.getOutputStream()); toProcess.writeBytes("exec " + s + "\n"); toProcess.flush(); } catch (final Exception e) { Log.e(TAG, "Exception while trying to run: '" + s + "' " + e.getMessage()); process = null; } return process; }

public CommandResult runWaitFor(final String s) { if (DEBUG) Log.d(TAG, "runWaitFor( " + s + " )"); final Process process = run(s); Integer exit_value = null; String stdout = null; String stderr = null; if (process != null) { try { exit_value = process.waitFor(); stdout = getStreamLines(process.getInputStream()); stderr = getStreamLines(process.getErrorStream()); } catch (final InterruptedException e) { Log.e(TAG, "runWaitFor " + e.toString()); } catch (final NullPointerException e) { Log.e(TAG, "runWaitFor " + e.toString()); } } return new CommandResult(exit_value, stdout, stderr); } }

public boolean canSU(final boolean force_check) { if (can_su == null || force_check) { final CommandResult r = su.runWaitFor("id"); final StringBuilder out = new StringBuilder(); if (r.stdout != null) { out.append(r.stdout).append(" ; "); } if (r.stderr != null) { out.append(r.stderr); } Log.d(TAG, "canSU() su[" + r.exit_value + "]: " + out); can_su = r.success(); } return can_su; }}
Sent from my GT-S5570 using XDA Premium 4 mobile app
 
KrauseDroid
Old
#13  
Junior Member - OP
Thanks Meter 0
Posts: 5
Join Date: Feb 2014
I managed to solve it already. I'm using this class now:
Code:
Select Code
public void RunAsRoot(String[] cmds){
        Process p = null;
		try {
			p = Runtime.getRuntime().exec("su");
		} catch (IOException e) {
			e.printStackTrace();
		}
        DataOutputStream os = new DataOutputStream(p.getOutputStream());            
        for (String tmpCmd : cmds) {
                try {
					os.writeBytes(tmpCmd+"\n");
				} catch (IOException e) {
					e.printStackTrace();
				}
        }           
        try {
			os.writeBytes("exit\n");
		} catch (IOException e) {
			e.printStackTrace();
		}  
        try {
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
Then simply call it via
Code:
Select Code
String[] commands = {"command1", "command2", "command3", ...};
                	RunAsRoot(commands);
Thanks anyways!
 
Masrepus
Old
#14  
Masrepus's Avatar
Senior Member
Thanks Meter 69
Posts: 635
Join Date: Feb 2013
Oh okay that looks really nice

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


XDA PORTAL POSTS

Change the Alarm Icon in the Status Bar with Xposed

It’s hard to find a more hated object than your alarm clock. They wake us up nearly … more

Return to Outer Space Outer Space with Ship Up Game

Since the release of Flappy Birds, we’ve had the “fortune” to see … more

Device Review: OnePlus One

Seemingly every day, there are a bunch of new devices being released. Some are budget devices and some are … more

Browse the Internet with the Innovative Atlas Web Browser

For the most part, Chrome, Firefox, and Dolphin Browser have no real rivals among … more