解决方案 »
- android,我如何通过已经得到的端口号获取使用该端口上网的应用程序的名称?急!
- 红色字体部分为什么这样写?
- 广告平台哪个好?哪个收入高?
- XML解析时,执行了startDocument,但是没有执行startElement,这是神马情况!求教~~~~
- gallery 添加文字说明!!谢谢
- 这个按钮总是出错
- 安卓做百度地图定位
- 在页面上,利用滑动手势,手指在屏幕上,由左至右滑动,也能能调出。同理,由右至左,也可以收起。
- 有人知道安卓系统源码如何下载,谢谢帮忙、
- com.google.process.gapps E/GCM: Missing checkin config file
- 请教高手们前辈们一个问题,学android源码有什么用?
- 【求助】ndk cygwin 编译C程序 头文件问题
01
这是方法,添加到你的应用程序中即可,比较简陋,呵呵!
02
public void execShell(String cmd){
03
try{
04
//权限设置
05
Process p = Runtime.getRuntime().exec("su");
06
//获取输出流
07
OutputStream outputStream = p.getOutputStream();
08
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
09
//将命令写入
10
dataOutputStream.writeBytes(cmd);
11
//提交命令
12
dataOutputStream.flush();
13
//关闭流操作
14
dataOutputStream.close();
15
outputStream.close();
16
}
17
catch(Throwable t)
18
{
19
t.printStackTrace();
20
}
21
}
2. [代码]第二种方式
01
public void execCommand(String command) throws IOException {
02
Runtime runtime = Runtime.getRuntime();
03
Process proc = runtime.exec(command);
04
try {
05
if (proc.waitFor() != 0) {
06
System.err.println("exit value = " + proc.exitValue());
07
}
08
BufferedReader in = new BufferedReader(new InputStreamReader(
09
proc.getInputStream()));
10
StringBuffer stringBuffer = new StringBuffer();
11
String line = null;
12
while ((line = in.readLine()) != null) {
13
stringBuffer.append(line+" ");
14
}
15
System.out.println(stringBuffer.toString());
16
17
} catch (InterruptedException e) {
18
System.err.println(e);
19
}finally{
20
try {
21
proc.destroy();
22
} catch (Exception e2) {
23
}
24
}
25
}