android4.2中用程序调用shell命令不成功?

echoJiang 2014-05-29 07:05:32
单位平台要升级成MTK4.2的方案,移植以前2.3的一堆遗老程序,发现个问题:Runtime.getRuntime().exec(command)在4.2中无效了,挺有趣的,大家探讨一下呗
	public static void execCommand(String command) {   
Process process = null;
DataOutputStream os = null;
try {
Log.e("*** DEBUG ***", "111111111111111111");
process = Runtime.getRuntime().exec("su");
Log.e("*** DEBUG ***", "222222222222222222");

InputStream inputstream = process.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);


Log.e("*** DEBUG ***", "3333333333333333331");

os = new DataOutputStream(process.getOutputStream());
//os.writeBytes(command+"\n");
os.writeBytes("ls \n");
os.writeBytes("exit\n");
os.flush();
Log.e("*** DEBUG ***", "444444444444444444444");

String line = "";

StringBuilder sb= new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
sb.append(line);
sb.append('\n');
}
//////////////
process.waitFor();
Log.e("*** DEBUG ***", "555555555555555 - " + sb.toString());
} catch (Exception e) {
Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());
//return false;
}

}


结果:
05-29 00:50:56.967: E/*** DEBUG ***(6244): 111111111111111111
05-29 00:50:57.060: E/*** DEBUG ***(6244): 222222222222222222
05-29 00:50:57.061: E/*** DEBUG ***(6244): 3333333333333333331
05-29 00:50:57.062: E/*** DEBUG ***(6244): 444444444444444444444
05-29 00:50:57.065: E/*** DEBUG ***(6244): 555555555555555 -
...全文
244 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
echoJiang 2014-06-03
  • 打赏
  • 举报
回复
引用 8 楼 han1202012 的回复:
我修改了下 : 不用加 "\n"
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Demo {

	public static void main(String[] args) {
		execCommand("ping www.baidu.com");
	}

	public static void execCommand(String command) {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec(command);

			InputStream inputstream = process.getInputStream();
			InputStreamReader inputstreamreader = new InputStreamReader(
					inputstream);
			BufferedReader bufferedreader = new BufferedReader(
					inputstreamreader);

			os = new DataOutputStream(process.getOutputStream());
			// os.writeBytes(command+"\n");
			os.writeBytes("ls \n");
			os.writeBytes("exit\n");
			os.flush();

			String line = "";

			StringBuilder sb = new StringBuilder(line);
			while ((line = bufferedreader.readLine()) != null) {
				sb.append(line);
				System.out.println(line);
				sb.append('\n');
			}
			process.waitFor();
		} catch (Exception e) {
		}

	}
}
执行结果 :
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_req=1 ttl=56 time=12.8 ms
64 bytes from 61.135.169.125: icmp_req=2 ttl=56 time=157 ms
64 bytes from 61.135.169.125: icmp_req=3 ttl=56 time=12.2 ms
64 bytes from 61.135.169.125: icmp_req=4 ttl=56 time=12.6 ms
64 bytes from 61.135.169.125: icmp_req=5 ttl=56 time=281 ms
64 bytes from 61.135.169.125: icmp_req=6 ttl=56 time=972 ms
64 bytes from 61.135.169.125: icmp_req=7 ttl=56 time=35.0 ms
64 bytes from 61.135.169.125: icmp_req=8 ttl=56 time=59.5 ms
64 bytes from 61.135.169.125: icmp_req=9 ttl=56 time=393 ms
64 bytes from 61.135.169.125: icmp_req=10 ttl=56 time=138 ms
64 bytes from 61.135.169.125: icmp_req=11 ttl=56 time=954 ms
代码是没问题的 看看手机里面有没有你需要的这些命令
辛苦了,谢谢大哥,在shell下手动执行su,确实看到没有返回success/fail,恐怕是su命令有问题
echoJiang 2014-06-03
  • 打赏
  • 举报
回复
引用 7 楼 echojiangyq 的回复:
[quote=引用 5 楼 linwhwylb 的回复:] process = Runtime.getRuntime().exec("su\n");
少个回车符么?我试一下[/quote] 加了回车符没有效果,在shell下手动执行su,确实看到没有返回success/fail,恐怕是su命令有问题
韩曙亮 2014-06-03
  • 打赏
  • 举报
回复
我修改了下 : 不用加 "\n"
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Demo {

	public static void main(String[] args) {
		execCommand("ping www.baidu.com");
	}

	public static void execCommand(String command) {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec(command);

			InputStream inputstream = process.getInputStream();
			InputStreamReader inputstreamreader = new InputStreamReader(
					inputstream);
			BufferedReader bufferedreader = new BufferedReader(
					inputstreamreader);

			os = new DataOutputStream(process.getOutputStream());
			// os.writeBytes(command+"\n");
			os.writeBytes("ls \n");
			os.writeBytes("exit\n");
			os.flush();

			String line = "";

			StringBuilder sb = new StringBuilder(line);
			while ((line = bufferedreader.readLine()) != null) {
				sb.append(line);
				System.out.println(line);
				sb.append('\n');
			}
			process.waitFor();
		} catch (Exception e) {
		}

	}
}
执行结果 :
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_req=1 ttl=56 time=12.8 ms
64 bytes from 61.135.169.125: icmp_req=2 ttl=56 time=157 ms
64 bytes from 61.135.169.125: icmp_req=3 ttl=56 time=12.2 ms
64 bytes from 61.135.169.125: icmp_req=4 ttl=56 time=12.6 ms
64 bytes from 61.135.169.125: icmp_req=5 ttl=56 time=281 ms
64 bytes from 61.135.169.125: icmp_req=6 ttl=56 time=972 ms
64 bytes from 61.135.169.125: icmp_req=7 ttl=56 time=35.0 ms
64 bytes from 61.135.169.125: icmp_req=8 ttl=56 time=59.5 ms
64 bytes from 61.135.169.125: icmp_req=9 ttl=56 time=393 ms
64 bytes from 61.135.169.125: icmp_req=10 ttl=56 time=138 ms
64 bytes from 61.135.169.125: icmp_req=11 ttl=56 time=954 ms
代码是没问题的 看看手机里面有没有你需要的这些命令
echoJiang 2014-06-03
  • 打赏
  • 举报
回复
引用 5 楼 linwhwylb 的回复:
process = Runtime.getRuntime().exec("su\n");
少个回车符么?我试一下
echoJiang 2014-06-03
  • 打赏
  • 举报
回复
引用 4 楼 tanwei4199 的回复:
不知道你想表达什么 你机器root了你的应用没有root权限啊 不信你把执行su之后的inputstream输出看看
确实没有考虑到这个问题,app获得的是systemUID,root权限要怎么获得呢..
Linux-Torvalds 2014-06-02
  • 打赏
  • 举报
回复
process = Runtime.getRuntime().exec("su\n");
echoJiang 2014-05-30
  • 打赏
  • 举报
回复
引用 1 楼 ivyvae 的回复:
没有权限?等大神。。
是真机 已经root过来
荒颜 2014-05-30
  • 打赏
  • 举报
回复
不知道你想表达什么 你机器root了你的应用没有root权限啊 不信你把执行su之后的inputstream输出看看
风吹得好舒服 2014-05-30
  • 打赏
  • 举报
回复
引用 2 楼 echojiangyq 的回复:
引用 1 楼 ivyvae 的回复:
没有权限?等大神。。
是真机 已经root过来
http://bbs.csdn.net/topics/390329421 看一下这个帖子吧。。
风吹得好舒服 2014-05-29
  • 打赏
  • 举报
回复
没有权限?等大神。。

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧