Most Android execution adb shell commands require root privileges, Android comes with the runtime. GetRuntime (). EXEC () is prone to error and finds a class that executes the adb shell command on the Web
The code is as follows:
/** Check if the phone has root privileges, send some messages */
Package com.dx.superbar.util;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Android.content.Context;
public class Rootcontext
{
private static Rootcontext instance = NULL;
private static Object MLock = new Object ();
String Mshell;
OutputStream o;
Process p;
Private Rootcontext (String cmd) throws Exception
{
This.mshell = cmd;
Init ();
}
public static Rootcontext getinstance ()
{
if (instance! = NULL)
{
return instance;
}
Synchronized (MLock)
{
Try
{
Instance = new Rootcontext ("Su");
}
catch (Exception e)
{
while (true)
Try
{
Instance = new Rootcontext ("/system/xbin/su");
}
catch (Exception E2)
{
Try
{
Instance = new Rootcontext ("/system/bin/su");
}
catch (Exception E3)
{
E3.printstacktrace ();
}
}
}
return instance;
}
}
private void Init () throws Exception
{
if ((THIS.P! = null) && (THIS.O! = null))
{
This.o.flush ();
This.o.close ();
This.p.destroy ();
}
THIS.P = Runtime.getruntime (). exec (This.mshell);
THIS.O = This.p.getoutputstream ();
System ("Ld_library_path=/vendor/lib:/system/lib");
}
private void System (String cmd)
{
Try
{
This.o.write (cmd + "\ n"). GetBytes ("ASCII"));
Return
}
catch (Exception e)
{
while (true)
Try
{
Init ();
}
catch (Exception E1)
{
E1.printstacktrace ();
}
}
}
public void RunCommand (String cmd)
{
System (CMD);
}
/**
* Judging if it's root
* */
public static Boolean hasrootaccess (Context ctx)
{
Final StringBuilder res = new StringBuilder ();
Try
{
if (Runcommandasroot (CTX, "Exit 0", res) = = 0)
return true;
}
catch (Exception e)
{
}
return false;
}
/**
* Run the command with root privileges
* */
public static int Runcommandasroot (Context ctx, String script,
StringBuilder Res)
{
Final file File = new file (Ctx.getcachedir (), "secopt.sh");
Final Scriptrunner runner = new Scriptrunner (file, script, res);
Runner.start ();
Try
{
Runner.join (40000);
if (Runner.isalive ())
{
Runner.interrupt ();
Runner.join (150);
Runner.destroy ();
Runner.join (50);
}
}
catch (Interruptedexception ex)
{
}
return runner.exitcode;
}
Private static final class Scriptrunner extends Thread
{
Private final file file;
Private final String script;
Private final StringBuilder Res;
public int exitcode =-1;
Private Process exec;
Public Scriptrunner (file file, String script, StringBuilder Res)
{
This.file = file;
this.script = script;
This.res = res;
}
@Override
public void Run ()
{
Try
{
File.createnewfile ();
Final String Abspath = File.getabsolutepath ();
Runtime.getruntime (). EXEC ("chmod 777" + Abspath). WaitFor ();
Final OutputStreamWriter out = new OutputStreamWriter (
New FileOutputStream (file));
if (New File ("/system/bin/sh"). Exists ())
{
Out.write ("#!/system/bin/sh\n");
}
Out.write (script);
if (!script.endswith ("\ n"))
Out.write ("\ n");
Out.write ("exit\n");
Out.flush ();
Out.close ();
exec = Runtime.getruntime (). EXEC ("su");
DataOutputStream OS = new DataOutputStream (
Exec.getoutputstream ());
Os.writebytes (Abspath);
Os.flush ();
Os.close ();
InputStreamReader r = New InputStreamReader (
Exec.getinputstream ());
Final char buf[] = new char[1024];
int read = 0;
while (read = R.read (BUF))! =-1)
{
if (res! = NULL)
Res.append (buf, 0, read);
}
R = new InputStreamReader (Exec.geterrorstream ());
Read = 0;
while (read = R.read (BUF))! =-1)
{
if (res! = NULL)
Res.append (buf, 0, read);
}
if (exec = null)
This.exitcode = Exec.waitfor ();
}
catch (Interruptedexception ex)
{
if (res! = NULL)
Res.append ("\noperation timed-out");
}
catch (Exception ex)
{
if (res! = NULL)
Res.append ("\ n" + ex);
}
Finally
{
Destroy ();
}
}
Public synchronized void Destroy ()
{
if (exec = null)
Exec.destroy ();
exec = null;
}
}
}
Android execute adb shell command