我们在app的开发过程中会遇到些需要系统支持的权限,这里有些是需要向系统申请的。写入这些申请的前提是该app安装的设备已经被破解了。
/**
* 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
*
* @param command 命令:String apkRoot="chmod 777 "+getPackageCodePath(); RootCommand(apkRoot);
* @return 应用程序是/否获取Root权限
*/
public boolean RootCommand(String command) {
Process process = null;
DataOutputStream os = null;
DataInputStream is = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
int aa = process.waitFor();
Logs.w("waitFor():"+aa);
is = new DataInputStream(process.getInputStream());
byte[] buffer = new byte[is.available()];
Logs.d("大小"+buffer.length);
is.read(buffer);
String out = new String(buffer);
Logs.e("返回:"+out);
} catch (Exception e) {
e.printStackTrace();
Logs.e(tag + "205:\n" + e);
return false;
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
Logs.e(tag + "217:\n" + e);
}
process.destroy();
}
Logs.d(tag + "222 SUCCESS");
return true;
}
调用方法
String apkRoot = "chmod 777 " + getPackageCodePath();//getPackageCodePath()来获得当前应用程序对应的 apk 文件的路径
boolean b = RootCommand(apkRoot);
Logs.v("获取Root权限:"+b);