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


  • 相关阅读:
    消费券
    .net Core 用户登入身份验证简单的demo
    微信阅读. 电脑版. 标记上一页阅读到的位置. 油猴(Tampermonkey)插件
    Docker.控制台程序.发布
    Docker.容器管理
    Docker.镜像管理
    RestSharp 加号变空格 + HTTP 请求
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
    数据库.Sqlserver.重建索引
    数据库.索引Vs树
  • 原文地址:https://www.cnblogs.com/exayong/p/6588028.html
Copyright © 2011-2022 走看看