大家好最近小弟想在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.为什么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”增加系统权限试试
谢谢哥们,增加系统权限以后,就可以输出内容了。还有一个问题,为什么用ifconfig eth0有内容输出,用ifconfig eth0 192.168.22.56这样设置IP确设置不了呢,设置之前我执行了su,全程没有报错的说,谢谢。
在这就是权限问题,希望能帮住你.
命令应该是写对了,我在shell里运行是可以设置的,很大可能性是你所说的权限问题,要运行linux命令,android的su权限可能还是不够的。netcfg可以用于查看,但真正设置还是要用到ifconfig的吧?