package com.android.utils;

 import java.io.File;

 import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; /**
* 本类主要用于在Java层执行Linux shell命令,获取一些系统下的信息
* 本例中的dmesg需要一些额外的权限才能使用
* 参考文章:
* 1. read android dmesg with code
* http://stackoverflow.com/questions/3643599/read-android-dmesg-with-code
* 2. Java执行带重定向或管道的shell命令的问题
* http://www.linuxidc.com/Linux/2012-07/64526.htm
*
* @author zengjf
*/
public class ShellExecute {
/**
* 本函数用于执行Linux shell命令
*
* @param command shell命令,支持管道,重定向
* @param directory 在指定目录下执行命令
* @return 返回shell命令执行结果
* @throws IOException 抛出IOException
*/
public static String execute ( String command, String directory )
throws IOException { // check the arguments
if (null == command)
return ""; if (command.trim().equals(""))
return ""; if (null == directory || directory.trim().equals(""))
directory = "/"; String result = "" ; List<String> cmds = new ArrayList<String>();
cmds.add("sh");
cmds.add("-c");
cmds.add(command); try {
ProcessBuilder builder = new ProcessBuilder(cmds); if ( directory != null )
builder.directory ( new File ( directory ) ) ; builder.redirectErrorStream (true) ;
Process process = builder.start ( ) ; //得到命令执行后的结果
InputStream is = process.getInputStream ( ) ;
byte[] buffer = new byte[1024] ;
while ( is.read(buffer) != -1 )
result = result + new String (buffer) ; is.close ( ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
return result.trim() ;
} /**
* 本函数用于执行Linux shell命令,执行目录被指定为:"/"
*
* @param command shell命令,支持管道,重定向
* @return 返回shell命令执行结果
* @throws IOException 抛出IOException
*/
public static String execute (String command) throws IOException { // check the arguments
if (null == command)
return ""; if (command.trim().equals(""))
return ""; return execute(command, "/");
} /**
* 本函数用于判断dmesg中是否存在pattern字符串,执行目录被指定为:"/"
*
* @param pattern 给grep匹配的字符串
* @return true: dmesg中存在pattern中的字符串<br>
* false:dmesg中不存在pattern中的字符串
* @throws IOException 抛出IOException
*/
public static boolean deviceExist(String pattern) throws IOException{ // check the arguments
if (null == pattern)
return false; if (pattern.trim().equals(""))
return false; return execute("dmesg | grep " + pattern).length() > 0;
}
}

最新文章

  1. for循环与for in,$(&#39;&#39;).each 与$.each的区别
  2. luogu2038[NOIP2014 T4]无线网络发射器选址
  3. 转:js中this关键字详解
  4. Sqlite实现默认时间为当前时间列的方法(转)
  5. SOA_环境安装系列1_Oracle SOA Suite11g安装总括(案例)
  6. c++代码模板
  7. 在PHPstorm编辑器中配置git环境
  8. 发布FireBird数据库所需要DLL文件
  9. 认识和理解css布局中的BFC
  10. java性能调优---------------------JVM调优方案
  11. Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略
  12. 聊聊Spring Cloud版本的那些事儿
  13. axios 拦截器统一在接口增加时间戳参数,防止走缓存。
  14. java(10)类的无参方法
  15. python3 第三十二章 - 标准库概览
  16. 《剑指offer》总结二 之二叉树
  17. 结合 Redis 实现同步锁
  18. 双节点weblogic集群安装
  19. Servlet 启动顺序
  20. C# Panel控件截图

热门文章

  1. Python day20正则表达式和re方法
  2. MKAnnotationView和MKPinAnnotationView的区别
  3. JS循环汇总
  4. JDBC 与 Bean Shell的使用(一)获取值,并且传递
  5. Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
  6. 『科学计算_理论』优化算法:梯度下降法&amp;牛顿法
  7. 高精度乘法,string中的坑
  8. substr和substring,slice和splice的区别,js字符串截取和数组截取
  9. Awk 从入门到放弃(4) — Aws 格式化
  10. 在mvc中弹出提示框