完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
shell命令工具类
包cn.hxg.androidgpiotest; 导入 java.io.BufferedReader; 导入 java.io.DataOutputStream; 导入 java.io.IOException; 导入 java.io.InputStreamReader; 导入android.util.Log; /** * 执行shell脚本工具类 * * @author Mountain */ public class CommandExecution { public static final String TAG = "CommandExecution"; 公共最终静态字符串 COMMAND_SU = "su"; 公共最终静态字符串 COMMAND_SH = "sh"; 公共最终静态字符串 COMMAND_EXIT = "exitn"; 公共最终静态字符串 COMMAND_LINE_END = "n"; /** * 命令执行结果 * * @author Mountain */ public static class CommandResult { public int result = -1; 公共字符串错误消息; 公共字符串成功消息; } /** * 执行命令—单条 * * @param command * @param isRoot * @return */ public static CommandResult execCommand(String command, boolean isRoot) { String[] commands = {command}; 返回 execCommand(命令,isRoot); } /** * 执行命令-多条 * * @param commands * @param isRoot * @return */ public static CommandResult execCommand(String[] commands, boolean isRoot) { CommandResult commandResult = new CommandResult(); if (commands == null || commands.length == 0) return commandResult; 进程进程 = null; 数据输出流 os = null; BufferedReader 成功结果 = null; BufferedReader errorResult = null; StringBuilder 成功消息 = null; StringBuilder errorMsg = null; 尝试 { 进程 = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); os = new DataOutputStream(process.getOutputStream()); 对于(字符串命令:命令){ if (command != null) { os.write(command.getBytes()); os.writeBytes(COMMAND_LINE_END); os.flush(); } } os.writeBytes(COMMAND_EXIT); os.flush(); commandResult.result = process.waitFor(); // 获取错误信息 successMsg = new StringBuilder(); errorMsg = new StringBuilder(); successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); 字符串 s; while ((s = successResult.readLine()) != null) successMsg.append(s); while ((s = errorResult.readLine()) != null) errorMsg.append(s); commandResult.successMsg = successMsg.toString(); commandResult.errorMsg = errorMsg.toString(); Log.i(TAG, commandResult.result + " | " + commandResult.successMsg + " | " + commandResult.errorMsg); } catch (IOException e) { String errmsg = e.getMessage(); if (errmsg != null) { Log.e(TAG, errmsg); } 其他 { e.printStackTrace(); } } 捕捉(异常 e){ 字符串 errmsg = e.getMessage(); if (errmsg != null) { Log.e(TAG, errmsg); } 其他 { e.printStackTrace(); } } 最后 { 尝试 { if (os != null) os.close(); if (successResult != null) successResult.close(); if (errorResult != null) errorResult.close(); } catch (IOException e) { String errmsg = e.getMessage(); if (errmsg != null) { Log.e(TAG, errmsg); } 其他 { e.printStackTrace(); } } if (process != null) process.destroy(); } 返回命令结果; } }
包cn.hxg.androidgpiotest; 导入android.os.Bundle; 导入android.os.SystemClock; 导入android.util.Log; 导入androidx.appcompat.app.AppCompatActivity; 公共类 MainActivity 扩展 AppCompatActivity { private String[] gpioShell = {"cd /sys/class/gpio", "echo 3 > export", "cd gpio3", "echo in >direction", "chmod 777 value"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 新线程(新可运行(){ @Override 公共无效运行(){ if (FileUtils.fileIsExists("/sys/class/gpio/gpio3")) { CommandExecution.CommandResult commandResult = CommandExecution.execCommand(gpioShell, true); Log.e("huang", commandResult.result + " | " + commandResult.successMsg + " | " + commandResult.errorMsg); while (true) { SystemClock.sleep(1000); String s = FileUtils.ReadTxtFile("/sys/class/gpio/gpio3/value"); Log.i("huang", "s===>" + s); } } } }).start();
包cn.hxg.androidgpiotest; 导入 java.io.BufferedReader; 导入java.io.File; 导入 java.io.FileInputStream; 导入 java.io.IOException; 导入 java.io.InputStream; 导入 java.io.InputStreamReader; public class FileUtils { //判断文件是否 存在public static boolean fileIsExists(String filePath) { try { File f = new File(filePath); if (!f.exists()) { return false; } } catch (Exception e) { return false; } 返回真; } /** * 读取文件 * * @param strFilePath * @return */ public static String ReadTxtFile(String strFilePath) { String path = strFilePath; 字符串内容 = ""; //文件内容字符串 //打开文件 File file = new File(path); if (!file.exists()) { //文件不存在 return content; } //如果路径是传递过来的参数,可以做一个非目录的判断 if (!file.isDirectory()) { try { InputStream instream = new FileInputStream(file); if (instream != null) { InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); 字符串线; //分行读取 while ((line = buffreader.readLine()) != null) { content += line + "n"; } instream.close(); } } catch (java.io.FileNotFoundException e) { } catch (IOException e) { } } 返回内容; } }
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
1106 浏览 0 评论
1278 浏览 1 评论
993 浏览 1 评论
2293 浏览 1 评论
3619 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-30 02:18 , Processed in 0.646194 second(s), Total 72, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号