当前位置: 代码迷 >> Android >> 请问一个在android下执行ifconfig命令的有关问题
  详细解决方案

请问一个在android下执行ifconfig命令的有关问题

热度:53   发布时间:2016-04-28 08:05:18.0
请教一个在android下执行ifconfig命令的问题
大家好

最近小弟想在android里,运行ifconfig lo命令,得到输出的信息,主要用的是Runtime.getRuntime().exec()这个方法,通过读取流得到输出。

尝试了好多次,结果都是空的,于是我在adb shell里使用ifconfig lo命令,发现是有输出值的。

我的问题是:
1.为什么shell里有输出,但我用自己的方法得不到呢?大大们有什么方法吗?
2.我的execRootCmd方法中,执行到r.exec("exit");这行时,会报无权限的异常,这是为嘛尼?

Thx in advance -v-

主要使用的方法如下:


// execute cmd
protected static Vector<String> execRootCmd(String paramString)
{
Vector<String> localVector = new Vector<String>();

DataInputStream dis = null;
Runtime r = Runtime.getRuntime();
try
{
r.exec("su"); // get root

StringBuilder sb = new StringBuilder();
Process p = r.exec(paramString);
InputStream input = p.getInputStream();
dis = new DataInputStream(input);

String content = null;
while ((content = dis.readLine()) != null)
{
sb.append(content).append("\n");
}
// r.exec("exit");

Log.i("UERY", "sb = " + sb.toString());
localVector.add(sb.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (dis != null)
{
try
{
dis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return localVector;
}

------解决方案--------------------
基于源码开发,可以编译的话:
1、在Android.mk中增加 LOCAL_CERTIFICATE := platform;
2、在AndroidManifest.xml中增加 androidSharedUserId=”android.uid.system”

增加系统权限试试
------解决方案--------------------
Android有个专门的命令好像叫NetCfg
------解决方案--------------------
可能是你的命令写错了.我以前也碰到这些问题,我是输出日志,我上次是多谢了adb三个字母,
在这就是权限问题,希望能帮住你.
  相关解决方案