在Android中有一个精简版的linux系统,因为是linux系统,那么有时候在写Android应用程序的时候会遇到权限问题。我们都知道在shell中可以通过chmod命令来修改权限,所以就希望通过程序来执行shell中的命令,如何来做呢?看下面代码:
private boolean runCommand(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
Log.i("command","The Command is : "+command);
process.waitFor();
} catch (Exception e) {
Log.w("Exception ", "Unexpected error - "+e.getMessage());
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {
Log.w("Exception ", "Unexpected error - "+e.getMessage());
}
}
return true;
}
然后通过调用这个方法就能实现在Android中修改文件权限,例如需要修改已安装apk的packagename的权限:
runCommand("chmod 777 /data/data/packagename");
如果有什么问题,请大家多多指正,谢谢。。
本文参考来自http://lazyhack.net/writing-an-android-root-application/的博客。