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;
    }
    }
    }

  • 相关阅读:
    day26 案例源码
    重踏学习Java路上_Day26(网络编程)
    多线程面试题
    day24--多线程案例源码
    重踏学习Java路上_Day24(多线程锁,线程组,设计模式)
    多线程之join方法 (转)
    有return的情况下try catch finally的执行顺序(转)
    day23--电影院买票问题解决 同步代码块 同步方法 静态同步方法的引入
    进程和线程的概述--day23配套
    [学习笔记] kd-tree
  • 原文地址:https://www.cnblogs.com/earendil/p/3956836.html
Copyright © 2011-2022 走看看