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

 

目录
相关文章
|
5月前
|
Shell Android开发
Android系统 adb shell push/pull 禁止特定文件
Android系统 adb shell push/pull 禁止特定文件
478 1
|
5月前
|
安全 Shell Android开发
Android系统 init.rc开机执行shell脚本
Android系统 init.rc开机执行shell脚本
856 0
|
API Android开发 计算机视觉
视觉智能平台有android人脸识别拍照demo?
视觉智能平台有android人脸识别拍照demo么?
99 0
|
Java Android开发 开发者
1024程序节|Android框架之一 BRVAH【BaseRecyclerViewAdapterHelper】使用demo
BRVAH是一个强大的RecyclerAdapter框架(什么是RecyclerView?),它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。为什么会有它?请查看「Android开源框架BRVAH由来篇」该框架于2016年4月10号发布的第1个版本到现在已经一年多了,经历了800多次代码提交,140多次版本打包,修复了1000多个问题,获得了9000多star,非常感谢大家的使用以及反馈。
235 0
|
2月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
67 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
24天前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
4月前
|
Shell 开发工具 Android开发
|
5月前
|
存储 安全 Shell
Android系统 adb shell auth授权使用
Android系统 adb shell auth授权使用
371 2
|
Java Android开发
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
|
Android开发
android和Flutter的混合工程Demo
Flutter和Android混合工程的启动逻辑与纯Flutter应用程序的启动逻辑略有不同。在混合工程中,您需要在Android项目中添加一些额外的代码来启动Flutter引擎并加载Flutter代码。以下是整个app的启动逻辑的详细解释
android和Flutter的混合工程Demo