欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 开发中使用Linux Shell实例详解

程序员文章站 2023-11-28 16:11:10
android 开发中使用linux shell实例详解 引言 android系统是基于linux内核运行的,而做为一名linux粉,不在android上面运行一下li...

android 开发中使用linux shell实例详解

引言

android系统是基于linux内核运行的,而做为一名linux粉,不在android上面运行一下linux shell怎么行呢?

最近发现了一个很好的android shell工具代码,在这里分享一下。

shell核心代码

import java.io.bufferedreader;
import java.io.dataoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.list;

/**
 * shellutils
 * <ul>
 * <strong>check root</strong>
 * <li>{@link shellutils#checkrootpermission()}</li>
 * </ul>
 * <ul>
 * <strong>execte command</strong>
 * <li>{@link shellutils#execcommand(string, boolean)}</li>
 * <li>{@link shellutils#execcommand(string, boolean, boolean)}</li>
 * <li>{@link shellutils#execcommand(list, boolean)}</li>
 * <li>{@link shellutils#execcommand(list, boolean, boolean)}</li>
 * <li>{@link shellutils#execcommand(string[], boolean)}</li>
 * <li>{@link shellutils#execcommand(string[], boolean, boolean)}</li>
 * </ul>
 */
public class shellutils {

  public static final string command_su    = "su";
  public static final string command_sh    = "sh";
  public static final string command_exit   = "exit\n";
  public static final string command_line_end = "\n";

  private shellutils() {
    throw new assertionerror();
  }

  /**
   * check whether has root permission
   * 
   * @return
   */
  public static boolean checkrootpermission() {
    return execcommand("echo root", true, false).result == 0;
  }

  /**
   * execute shell command, default return result msg
   * 
   * @param command command
   * @param isroot whether need to run with root
   * @return
   * @see shellutils#execcommand(string[], boolean, boolean)
   */
  public static commandresult execcommand(string command, boolean isroot) {
    return execcommand(new string[] {command}, isroot, true);
  }

  /**
   * execute shell commands, default return result msg
   * 
   * @param commands command list
   * @param isroot whether need to run with root
   * @return
   * @see shellutils#execcommand(string[], boolean, boolean)
   */
  public static commandresult execcommand(list<string> commands, boolean isroot) {
    return execcommand(commands == null ? null : commands.toarray(new string[] {}), isroot, true);
  }

  /**
   * execute shell commands, default return result msg
   * 
   * @param commands command array
   * @param isroot whether need to run with root
   * @return
   * @see shellutils#execcommand(string[], boolean, boolean)
   */
  public static commandresult execcommand(string[] commands, boolean isroot) {
    return execcommand(commands, isroot, true);
  }

  /**
   * execute shell command
   * 
   * @param command command
   * @param isroot whether need to run with root
   * @param isneedresultmsg whether need result msg
   * @return
   * @see shellutils#execcommand(string[], boolean, boolean)
   */
  public static commandresult execcommand(string command, boolean isroot, boolean isneedresultmsg) {
    return execcommand(new string[] {command}, isroot, isneedresultmsg);
  }

  /**
   * execute shell commands
   * 
   * @param commands command list
   * @param isroot whether need to run with root
   * @param isneedresultmsg whether need result msg
   * @return
   * @see shellutils#execcommand(string[], boolean, boolean)
   */
  public static commandresult execcommand(list<string> commands, boolean isroot, boolean isneedresultmsg) {
    return execcommand(commands == null ? null : commands.toarray(new string[] {}), isroot, isneedresultmsg);
  }

  /**
   * execute shell commands
   * 
   * @param commands command array
   * @param isroot whether need to run with root
   * @param isneedresultmsg whether need result msg
   * @return <ul>
   *     <li>if isneedresultmsg is false, {@link commandresult#successmsg} is null and
   *     {@link commandresult#errormsg} is null.</li>
   *     <li>if {@link commandresult#result} is -1, there maybe some excepiton.</li>
   *     </ul>
   */
  public static commandresult execcommand(string[] commands, boolean isroot, boolean isneedresultmsg) {
    int result = -1;
    if (commands == null || commands.length == 0) {
      return new commandresult(result, null, null);
    }

    process process = null;
    bufferedreader successresult = null;
    bufferedreader errorresult = null;
    stringbuilder successmsg = null;
    stringbuilder errormsg = null;

    dataoutputstream os = null;
    try {
      process = runtime.getruntime().exec(isroot ? command_su : command_sh);
      os = new dataoutputstream(process.getoutputstream());
      for (string command : commands) {
        if (command == null) {
          continue;
        }

        // donnot use os.writebytes(commmand), avoid chinese charset error
        os.write(command.getbytes());
        os.writebytes(command_line_end);
        os.flush();
      }
      os.writebytes(command_exit);
      os.flush();

      result = process.waitfor();
      // get command result
      if (isneedresultmsg) {
        successmsg = new stringbuilder();
        errormsg = new stringbuilder();
        successresult = new bufferedreader(new inputstreamreader(process.getinputstream()));
        errorresult = new bufferedreader(new inputstreamreader(process.geterrorstream()));
        string s;
        while ((s = successresult.readline()) != null) {
          successmsg.append(s);
        }
        while ((s = errorresult.readline()) != null) {
          errormsg.append(s);
        }
      }
    } catch (ioexception e) {
      e.printstacktrace();
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      try {
        if (os != null) {
          os.close();
        }
        if (successresult != null) {
          successresult.close();
        }
        if (errorresult != null) {
          errorresult.close();
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }

      if (process != null) {
        process.destroy();
      }
    }
    return new commandresult(result, successmsg == null ? null : successmsg.tostring(), errormsg == null ? null
        : errormsg.tostring());
  }

  /**
   * result of command
   * <ul>
   * <li>{@link commandresult#result} means result of command, 0 means normal, else means error, same to excute in
   * linux shell</li>
   * <li>{@link commandresult#successmsg} means success message of command result</li>
   * <li>{@link commandresult#errormsg} means error message of command result</li>
   * </ul>
   */
  public static class commandresult {

    /** result of command **/
    public int  result;
    /** success message of command result **/
    public string successmsg;
    /** error message of command result **/
    public string errormsg;

    public commandresult(int result) {
      this.result = result;
    }

    public commandresult(int result, string successmsg, string errormsg) {
      this.result = result;
      this.successmsg = successmsg;
      this.errormsg = errormsg;
    }
  }
}

shellutils代码引用自:trinea

小实例

是否root

public boolean isrooted(){
  commandresult cmdresult = shellutils.execcommand("su", true);
  if (cmdresult.errormsg.equals("permission denied") || cmdresult.result != 0) {

    return false;
  }else{
    return true;
  }
}

复制文件

string[] commands = new string[] { "mount -o rw,remount /system", "cp /mnt/sdcard/xx.apk /system/app/" };

public boolean copyfile(string[] cmdtext){
  commandresult cmdresult = shellutils.execcommand(cmdtext, true);
  if (cmdresult.errormsg.equals("permission denied") || cmdresult.result != 0) {
    return false;
  }else{
    return true;
  }
}

我暂时就举这两个例子,只要你会shell,什么操作都是可以的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!