当前位置:   article > 正文

Android 通过代码执行Linux 命令 echo 命令_android echo 0x1

android echo 0x1

使用安装封装 runtime
echo(new String[]{“echo 1 > /sys/bus/text”}); //调用方式
private void echo(String[] command) {
DualExecutor.getExecutorService().execute(() -> {
try {
Process process = Runtime.getRuntime().exec(“sh”);
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : command) {
Log.d(TAG,“tmpCmd:”+tmpCmd);
outputStream.writeBytes(tmpCmd + “\n”);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, “ex” + e.toString());
}

    });
}
  • 1
  • 2

使用java 封装类 ProcessBuilder
//d调用方法
String[] cmds = {“logcat”, “|”, “grep”, “test”};
onCmd(cmds);
/**
* 执行MFD 工具
*
* @param cmds
*/
private void onCmd(final String[] cmds) {
Log.e(TAG, “onCmd”);
DualExecutor.getExecutorService().execute(() -> {
ProcessBuilder processBuilder = new ProcessBuilder(cmds);
processBuilder.redirectErrorStream(true);
Process process = null;
try {
process = processBuilder.start();
printCmdLog(process.getInputStream());
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, “CMD 异常” + e.toString());
}
});

}
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/160687
推荐阅读
相关标签
  

闽ICP备14008679号