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;
}
}

Android shell command execute Demo的更多相关文章

  1. ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137

    错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...

  2. I.MX6 Android shutdown shell command

    /******************************************************************************* * I.MX6 Android shu ...

  3. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  4. Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)

    一.可用环境变量列表(以下来自google翻译): BRANCH_NAME 对于多分支项目,这将被设置为正在构建的分支的名称,例如,如果您希望从而master不是从特征分支部署到生产. CHANGE_ ...

  5. android: shell 命令

    adb是Android重要工具之一,以提供强大的特性,例如复制文件到设备或从设备复制文件.可以使用Android Shell命令行参数连接到手机本身,并发送基本的 shell 命令. 进入命令行,使用 ...

  6. Android第一代壳demo编写

    Android第一代壳Demo编写 前言 这篇文章是对姜维大佬的这篇文章[Android中的Apk的加固(加壳)原理解析和实现]的补充.建议先看一编姜维大佬的这篇文章再看. 姜维大佬写那篇文章的时间距 ...

  7. Android java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver

    java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver 解决 ...

  8. 解决Android微信支付官方demo运行失败

    Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...

  9. com.android.dx.command.Main with arguments

    Error:Execution failed for task ':jingyeyun:transformClassesWithDexForDebug'.> com.android.build. ...

随机推荐

  1. shell逻辑运算符

    逻辑运算符 以下介绍 Shell 的逻辑运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 && 逻辑的 AND [[ $a -lt 100 && ...

  2. win10 鼠标指针

    https://www.ithome.com/html/zhuti/26449.htm

  3. Selenium库的使用

    一.什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行 ...

  4. Qt_QString::split测试

    1. #define GID_PREFIX "dr_" QString str = "dr__awedr4"; QString str1; QStringLis ...

  5. C# DataTable列名不区分大小写

    一直很纠结的就是DataTable的列名如何才能规范,从Oracle取出的DataTable都是大写,最后尝试了一下,原来C#的DataTable列名并不区分大小写,具体例子如下: DataTable ...

  6. .net 获取邮箱邮件列表和内容

    需求: 最近项目里遇到了个问题,对方没有提供直接获取数据源的api接口,而是通过邮件发数据的.这就需要接收邮件后解析邮件内容获得数据. 分析: 想在代码里实时获取邮件,可以通过邮件的几个协议来实现,常 ...

  7. Oracle性能诊断艺术-读书笔记(脚本execution_plans截图)

  8. 附录A——面向对象基础

    在学习设计模式之前,C#语言中一些基本的面向对象的知识还是应该具备的,比如像继承.多态,接口.抽象类,集合.泛型等. A.2 类与实例 什么是对象? 一切事物(事和物)都是对象,对象就是可以看到.感觉 ...

  9. keras-anomaly-detection 代码分析——本质上就是SAE、LSTM时间序列预测

    keras-anomaly-detection Anomaly detection implemented in Keras The source codes of the recurrent, co ...

  10. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...