zoukankan      html  css  js  c++  java
  • android 应用程序中执行Linux 命令

    ADB 无线调试命令
    son =
    "setprop service.adb.tcp.port 5555 " +

    "stop adbd " +
    "start adbd ";
    soff = "setprop service.adb.tcp.port -1 " +
    "stop adbd " +
    "start adbd ";
    reboot 立即重启

    //获取Ip
    String getIp() {

    String ip = "127.0.0.1";
    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    if (!wm.isWifiEnabled()) {
    wm.setWifiEnabled(true);
    }
    WifiInfo wi = wm.getConnectionInfo();
    int addr = wi.getIpAddress();
    return intToIp(addr);
    }

    String intToIp(int t) {
    return (t & 0xFF) + "." + ((t >> 8) & 0xFF) + "." + ((t >> 16) & 0xFF) + "." + ((t >> 24) & 0xFF);
    }

    //执行普通命令
    String exec(String cmd) {
    if (cmd.startsWith("su")|cmd.startsWith("ping")) {
    return "不允许执行的命令";
    }
    String result = "";
    Process ps = null;

    ProcessBuilder pb = new ProcessBuilder(cmd);
    InputStream es = null;
    InputStream is = null;
    try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int read = -1;
    ps = pb.start();
    es = ps.getErrorStream();
    while ((read = es.read()) != -1) {
    baos.write(read);

    }
    baos.write(' ');
    is = ps.getInputStream();
    while ((read = is.read()) != -1) {
    baos.write(read);

    }
    byte[] data = baos.toByteArray();
    result = new String(data);
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {


    if (es != null) {
    es.close();
    }

    if (is != null) {
    is.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    if (ps != null) {
    ps.destroy();
    }
    }
    return result;
    }
    //执行root命令
    String rootExec(String cmd) {
    if (cmd.startsWith("su")|cmd.startsWith("ping")) {
    return "不允许执行的命令";
    }
    String result = "";
    DataOutputStream dos = null;
    DataInputStream dis = null;
    try {
    Process ps = Runtime.getRuntime().exec("su");
    dos = new DataOutputStream(ps.getOutputStream());
    dis = new DataInputStream(ps.getInputStream());
    dos.writeBytes(cmd + " ");
    dos.flush();
    dos.writeBytes("exit ");
    dos.flush();
    String line = null;
    while ((line = dis.readLine())!=null) {
    result += " "+line;
    // Message ms = new Message();
    // ms.obj = line;
    // handler.sendMessageDelayed(ms,1000);
    }

    ps.waitFor();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (dos != null) {


    try {
    dos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (dis != null) {


    try {
    dis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }


    }
    return result;
    }


  • 相关阅读:
    Windows 下Nexus搭建Maven私服
    WebService中获取request对象一例
    利用window.navigator.userAgent判断当前是否微信内置浏览器
    批量插入写法
    MySql 使用递归函数时遇到的级联删除问题
    【Mysql】 你会用 information_schema吗?
    Volatile 多线程中用到的关键字
    spring+springMVC中使用@Transcational方式管理事务的必须要配的东西。
    Android中操作SQLite数据库
    Oracle中的字符处理方法
  • 原文地址:https://www.cnblogs.com/exayong/p/6588028.html
Copyright © 2011-2022 走看看