zoukankan      html  css  js  c++  java
  • Android 执行 adb shell 命令

    Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类

    代码如下:

    /**检查手机是否存在root权限,发送一些消息*/
    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 + " ").getBytes("ASCII"));
    return;
    }
    catch (Exception e)
    {
    while (true)
    try
    {
    init();
    }
    catch (Exception e1)
    {
    e1.printStackTrace();
    }
    }
    }

    public void runCommand(String cmd)
    {
    system(cmd);
    }

    /**
    * 判断是否已经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;
    }

    /**
    * 以root的权限运行命令
    * */
    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 ");
    }
    out.write(script);
    if (!script.endsWith(" "))
    out.write(" ");
    out.write("exit ");
    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(" Operation timed-out");
    }
    catch (Exception ex)
    {
    if (res != null)
    res.append(" " + ex);
    }
    finally
    {
    destroy();
    }
    }

    public synchronized void destroy()
    {
    if (exec != null)
    exec.destroy();
    exec = null;
    }
    }
    }

  • 相关阅读:
    How to install VXDIAG Honda, Toyota and JLR SDD software
    16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
    Cummins INSITE locked and ask for verification code
    How to use BMW Multi Tool 7.3 to replace lost key for BMW X1
    Bleed Brake Master Cylinder with Intelligent Tester IT2
    Porsche Piwis Tester II “No VCI has been detected”,how to do?
    Creader VIII VS. Creader VII+
    How to solve GM MDI cannot complete the installation
    汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
    汽车节温器单片机开发思路
  • 原文地址:https://www.cnblogs.com/earendil/p/3956836.html
Copyright © 2011-2022 走看看