android调用shell命令及权限问题

本文详细介绍了Android中如何通过Runtime类调用shell命令,包括简单命令和带参数命令的执行方式,并讨论了在执行如tar、unzip等复杂命令时可能遇到的权限问题。同时,文章区分了apk运行时权限与文件系统权限的不同,分析了Android的权限规则,如基于签名的权限、基于UserID的进程安全机制、显式权限声明等,以及如何处理权限不足导致的问题。
摘要由CSDN通过智能技术生成

android中调用shell命令一般通过这就是Runtime类,在Runtime类里提供了获取当前运行环境的接口。

其中的exec函数返回一个执行shell命令的子进程。exec函数的具体实现形式有以下几种:
public Process exec(String command) throws IOException
public Process exec(String command,String[] envp) throws
IOException
public Process exec(String command,String[] envp,File dir) throws
IOException
public Process exec(String[] cmdarray) throws IOException
public Process exec(String[] cmdarray, String[] envp) throws
IOException
public Process exec(String[] cmdarray, String[] envp,File dir)
throws IOException

 

1)执行简单的命令的方法:
代码如下:    
try           
String commands = "ls -l";       
Process process = Runtime.getRuntime().exec (commands);      
// for showing the info on screen         
InputStreamReader ir=new
InputStreamReader(process.getInputStream());      
BufferedReader input = new BufferedReader (ir);       
String line;       
while ((line = input.readLine ()) != null){          
System.out.println(line);  
}//end try    
catch (java.io.IOException e){         
System.err.println ("IOException " + e.getMessage());     
}  

2)执行带有参数的命令(尤其是参数需要用引号的)时则需要用String的数组来表示整个命令,而且要用转义符把引号的特殊含义去除,例如我们要执行
find / -name "*mysql*" -print 时,用如下代码     
try           
String[] commands = new
String[]{"find","

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值