Android shell command execute Demo
2024-10-15 02:17:02
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;
}
}
最新文章
- for循环与for in,$(&#39;&#39;).each 与$.each的区别
- luogu2038[NOIP2014 T4]无线网络发射器选址
- 转:js中this关键字详解
- Sqlite实现默认时间为当前时间列的方法(转)
- SOA_环境安装系列1_Oracle SOA Suite11g安装总括(案例)
- c++代码模板
- 在PHPstorm编辑器中配置git环境
- 发布FireBird数据库所需要DLL文件
- 认识和理解css布局中的BFC
- java性能调优---------------------JVM调优方案
- Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略
- 聊聊Spring Cloud版本的那些事儿
- axios 拦截器统一在接口增加时间戳参数,防止走缓存。
- java(10)类的无参方法
- python3 第三十二章 - 标准库概览
- 《剑指offer》总结二 之二叉树
- 结合 Redis 实现同步锁
- 双节点weblogic集群安装
- Servlet 启动顺序
- C# Panel控件截图
热门文章
- Python day20正则表达式和re方法
- MKAnnotationView和MKPinAnnotationView的区别
- JS循环汇总
- JDBC 与 Bean Shell的使用(一)获取值,并且传递
- Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
- 『科学计算_理论』优化算法:梯度下降法&;牛顿法
- 高精度乘法,string中的坑
- substr和substring,slice和splice的区别,js字符串截取和数组截取
- Awk 从入门到放弃(4) — Aws 格式化
- 在mvc中弹出提示框