Android execute adb shell command

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.