Android shell command execute Demo
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的更多相关文章
- 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 ...
- I.MX6 Android shutdown shell command
/******************************************************************************* * I.MX6 Android shu ...
- 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 ...
- Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)
一.可用环境变量列表(以下来自google翻译): BRANCH_NAME 对于多分支项目,这将被设置为正在构建的分支的名称,例如,如果您希望从而master不是从特征分支部署到生产. CHANGE_ ...
- android: shell 命令
adb是Android重要工具之一,以提供强大的特性,例如复制文件到设备或从设备复制文件.可以使用Android Shell命令行参数连接到手机本身,并发送基本的 shell 命令. 进入命令行,使用 ...
- Android第一代壳demo编写
Android第一代壳Demo编写 前言 这篇文章是对姜维大佬的这篇文章[Android中的Apk的加固(加壳)原理解析和实现]的补充.建议先看一编姜维大佬的这篇文章再看. 姜维大佬写那篇文章的时间距 ...
- 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 解决 ...
- 解决Android微信支付官方demo运行失败
Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...
- com.android.dx.command.Main with arguments
Error:Execution failed for task ':jingyeyun:transformClassesWithDexForDebug'.> com.android.build. ...
随机推荐
- Springboot 如何加密,以及利用Swagger2构建Restful API
先看一下使用Swagger2构建Restful API效果图 超级简单的,只需要在pom 中引用如下jar包 <dependency> <groupId>io.springfo ...
- 02_DllZZ.def
ZC: 在VC6里面,只要有这个文件就可以了.但是到了 VS2010,需要手动的指定使用这个文件才行:VS2010-->项目-->属性--> 来到窗口"??? 属性页&qu ...
- VS2013_CodeLens
CodeLens 只有VS2013 旗舰版 (update 2及以上) 才可以用,高级版 专业版都没有. 如何打开CodeLens呢?在VS菜单栏 >> 工具 >> 选项 &g ...
- SpringBoot+Mybatis-Generator自动生成
原文链接 1.版本 Spring Boot 1.5.10 mybatis-generator-core 1.3.5 mybatis-generator-maven-plugin 1.3.5 2.项目目 ...
- LeetCode--053--最大子序和
问题描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: ...
- CF 711B - Chris and Magic Square
挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...
- 双十一用python秒杀京东好货!
好久没用python了,都写不来了. 需要用到selenium 和 Chromedriver: 我只是记录一下几个坑: 第一个坑:自己电脑里安装了两个版本的python ,3.5和3.6 结果我在pi ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- forget word out4
1★ be 使~ 成为: 2★ bene bene 3★ bi 2,两个,双重 4★ by 在~ 旁边,副的
- app.jsNodejs启动测试服务
'use strict'; var express = require('express');var app = express('');var fs = require('fs'); app.get ...