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


  • 相关阅读:
    python编码问题和py2和py3的不同
    day27
    多继承补充
    zoj3820 Building Fire Stations 树的中心
    DLX舞蹈链 hdu5046
    时间复杂度
    线性求中位数 poj2388
    codeforce447 D SGU 548 贪心+优先队列
    hdu4864 hdu4268 贪心 lower_bound
    zoj3672 Gao The Sequence
  • 原文地址:https://www.cnblogs.com/exayong/p/6588028.html
Copyright © 2011-2022 走看看