Android shell command execute Demo

  1 package com.android.utils;
  2
  3
  4 import java.io.File;
  5
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10
 11 /**
 12  * 本类主要用于在Java层执行Linux shell命令,获取一些系统下的信息
 13  * 本例中的dmesg需要一些额外的权限才能使用
 14  * 参考文章:
 15  *    1. read android dmesg with code
 16  *        http://stackoverflow.com/questions/3643599/read-android-dmesg-with-code
 17  *    2. Java执行带重定向或管道的shell命令的问题
 18  *        http://www.linuxidc.com/Linux/2012-07/64526.htm
 19  *
 20  * @author zengjf
 21  */
 22 public class ShellExecute {
 23     /**
 24      * 本函数用于执行Linux shell命令
 25      *
 26      * @param command                 shell命令,支持管道,重定向
 27      * @param directory               在指定目录下执行命令
 28      * @return                        返回shell命令执行结果
 29      * @throws IOException            抛出IOException
 30      */
 31     public static String execute ( String command, String directory )
 32             throws IOException {
 33
 34         // check the arguments
 35         if (null == command)
 36             return "";
 37
 38         if (command.trim().equals(""))
 39             return "";
 40
 41         if (null == directory || directory.trim().equals(""))
 42             directory = "/";
 43
 44         String result = "" ;
 45
 46         List<String> cmds = new ArrayList<String>();
 47         cmds.add("sh");
 48         cmds.add("-c");
 49         cmds.add(command);
 50
 51         try {
 52             ProcessBuilder builder = new ProcessBuilder(cmds);
 53
 54             if ( directory != null )
 55                 builder.directory ( new File ( directory ) ) ;
 56
 57             builder.redirectErrorStream (true) ;
 58             Process process = builder.start ( ) ;
 59
 60             //得到命令执行后的结果
 61             InputStream is = process.getInputStream ( ) ;
 62             byte[] buffer = new byte[1024] ;
 63             while ( is.read(buffer) != -1 )
 64                 result = result + new String (buffer) ;
 65
 66             is.close ( ) ;
 67         } catch ( Exception e ) {
 68             e.printStackTrace ( ) ;
 69         }
 70         return result.trim() ;
 71     }
 72
 73     /**
 74      * 本函数用于执行Linux shell命令,执行目录被指定为:"/"
 75      *
 76      * @param command                 shell命令,支持管道,重定向
 77      * @return                        返回shell命令执行结果
 78      * @throws IOException            抛出IOException
 79      */
 80     public static String execute (String command) throws IOException {
 81
 82         // check the arguments
 83         if (null == command)
 84             return "";
 85
 86         if (command.trim().equals(""))
 87             return "";
 88
 89         return execute(command, "/");
 90     }
 91
 92     /**
 93      * 本函数用于判断dmesg中是否存在pattern字符串,执行目录被指定为:"/"
 94      *
 95      * @param pattern         给grep匹配的字符串
 96      * @return                true:  dmesg中存在pattern中的字符串<br>
 97      *                        false:dmesg中不存在pattern中的字符串
 98      * @throws IOException    抛出IOException
 99      */
100     public static boolean deviceExist(String pattern) throws IOException{
101
102         // check the arguments
103         if (null == pattern)
104             return false;
105
106         if (pattern.trim().equals(""))
107             return false;
108
109         return execute("dmesg | grep " + pattern).length() > 0;
110     }
111 }

 

时间: 2024-08-01 12:04:39

Android shell command execute Demo的相关文章

29.13. parallel - build and execute shell command lines from standard input in parallel

并行执行shell命令 $ sudo apt-get install parallel 例 29.5. parallel - build and execute shell command lines from standard input in parallel $ cat *.csv | parallel --pipe grep '13113' 设置块大小 $ cat *.csv | parallel --block 10M --pipe grep '131136688' 原文出处:Netk

I.MX6 Android shutdown shell command

/******************************************************************************* * I.MX6 Android shutdown shell command * 说明: * 想在I.MX6上执行shell关机命令,不过效果貌似不是很好,开关键多按两下又亮了. * * 2016-7-25 深圳 南山平山村 曾剑锋 ****************************************************

7.13. parallel - build and execute shell command lines from standard input in parallel

并行执行shell命令 $ sudo apt-get install parallel 例 7.5. parallel - build and execute shell command lines from standard input in parallel $ cat *.csv | parallel --pipe grep '13113' 设置块大小 $ cat *.csv | parallel --block 10M --pipe grep '131136688' 原文出处:Netki

android studio、eclipse分别导入新浪微博 Android sdk 第三方登录demo

1.下载解压sdk 先下载weibo android sdk 包: 打开网址:https://github.com/sinaweibosdk/weibo_android_sdk 将sdk下载到本地,解压后的目录结构: 点击进入"demo-src"目录,结构如下: 将上面的两个项目都导入到eclipse中!! 2.导demo到eclipse 分别修改两个项目的文件编码为UTF-8,保存. 3.修改 debug.keystore MD5 工具是根据 keystore 来生成签名的,丌同的

第 5 章 Shell command

目录 5.1. Help Commands 5.1.1. man - an interface to the on-line reference manuals 5.1.1.1. manpath.config 5.1.1.2. 查看man手册位置 5.1.1.3. 指定手册位置 5.2. getconf - Query system configuration variables 5.3. Directory and File System Related 5.3.1. dirname 5.3.

android-跪求Android集成谷歌地图demo

问题描述 跪求Android集成谷歌地图demo 需要eclipse版本的能能运行,官方demo我下过,没报错,但是一运行我的eclipse就崩溃了.唉....都不知道什么错弄蹦的 解决方案 用百度地图吧,我今天刚好弄了一个demo大集合,运行完全木有问题,很好用... 解决方案二: 人家发给你也不一定有用啊.还是设置环境啊.具体你那里出了问题可以说出来. 解决方案三: http://stackoverflow.com/questions/16305685/java-lang-noclassde

Android shell命令行中过滤adb logcat输出的方法_Android

        我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adb logcat输出的方法.        1.只显示需要的输出(白名单)        最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep 强大的正则表达式匹配.简单的匹配一行当中的某个字符串,例如 MyApp:        adb logcat | grep MyAp

Android shell命令行中过滤adb logcat输出的几种方法_Android

我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adb logcat输出的方法.        1.只显示需要的输出(白名单)        最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep 强大的正则表达式匹配.简单的匹配一行当中的某个字符串,例如 MyApp:        adb logcat | grep MyApp       

Android shell命令行中过滤adb logcat输出的方法

我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adb logcat输出的方法. 1.只显示需要的输出(白名单) 最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep 强大的正则表达式匹配.简单的匹配一行当中的某个字符串,例如 MyApp: adb logcat | grep MyApp        adb logcat | grep -i