android Shell指令

1:添加sd快捷方式java

String oldPath = file.getPath();
Slog.e("m_tag", "oldPath=" + oldPath);
String cmd = "ln -s " + oldPath + " /storage/hdd";
String[] cmds = {"su", "-c", cmd};
Slog.e("m_tag", "cmd=" + cmd);
execShellCmd(cmds);

oldPath 是sd的路徑app

/storage/hdd 是你須要映射的路徑ui

具體方法爲:spa

private String execShellCmd(String[] cmds) {
   Runtime runtime = Runtime.getRuntime();
   try {
      java.lang.Process p = runtime.exec(cmds);
      if (p.waitFor() != 0) {
         Slog.e(TAG, "exit value = " + p.exitValue());
      }
      BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = br.readLine()) != null) {
         sb.append(line + "-");
      }
      Slog.e(TAG, "serial port back data:" + sb.toString());
      return sb.toString();
   } catch (Exception e) {
      Slog.e(TAG, "execShellCmd error:" + e.toString());
      e.printStackTrace();
   }
   return "null";
}

執行完後,經過路徑/storage/hdd 也可找到sd卡get

 

二:向串口寫數據cmd

String[] cmds = new String[3];
cmd[0] = "sh";
cmd[1] = "-c";
cmd[2] = "echo -en \'\\x11\' > /dev/ttyS3";

x11 你須要向串口寫的數據it

/dev/ttyS3 端口號io

一樣調用 execShellCmd(cmds )便可file

相關文章
相關標籤/搜索