admin管理员组文章数量:1571492
背景:
做一个应用,需求是获取root权限后可以停用/启用某些应用,包括系统应用!停用的应用将不在设置-->应用中展示,大部分系统自带的桌面应该也不会再显示!
如果是用menifest.xml中添加android:sharedUserId="android.uid.system" ,需要包的签名与系统的签名一致!
所以考虑使用shell命令执行!
1.获取root权限:String cmd="chmod 777 " + pkgCodePath;
/**
* 获取root权限
* @param pkgCodePath 通过getPackageCodePath() 获取
* @return
*/
public static boolean getRootPermission(String pkgCodePath){
Process process = null;
DataOutputStream os = null;
try {
String cmd="chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
2.判断root权限获取结果:ls -l pkgCodePath
/**
* 判断某个路径的app的权限
* @param pkgCodePath
* @return
*/
private static boolean checkRoot(String pkgCodePath){
Process process = null;
try {
String cmd="ls -l " + pkgCodePath;
process = Runtime.getRuntime().exec(cmd);
BufferedReader mReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer mRespBuff = new StringBuffer();
char[] buff = new char[1024*10];
int ch = 0;
while((ch = mReader.read(buff)) != -1){
mRespBuff.append(buff, 0, ch);
}
mReader.close();
String regx="-rwxr[\\s\\S]+";//root后权限的表达式
return Pattern.matches(regx, mRespBuff.toString());
} catch (Exception e) {
System.out.println("checkRoot exception");
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.停用应用:String cmd="pm disable " + packageName;
/**
* 停用应用
* @param context 上下文信息
* @param packageName 应用的包名
* @return
*/
public static boolean invalidApp(Context context,String packageName){
if(!getRootPermission(context.getPackageCodePath())){
Toast.makeText(context, "冻结应用需要root权限", 0).show();
return false;
}
Process process = null;
DataOutputStream os = null;
try {
String cmd="pm disable " + packageName;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Toast.makeText(context, "冻结应用失败", 0).show();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Toast.makeText(context, "冻结应用成功", 0).show();
return true;
}
4.启用应用:String cmd="pm enable " + packageName;
/**
* 启用应用
* @param context
* @param packageName
* @return
*/
public static boolean validApp(Context context,String packageName){
if(!getRootPermission(context.getPackageCodePath())){
Toast.makeText(context, "解除冻结需要root权限", 0).show();
return false;
}
Process process = null;
DataOutputStream os = null;
try {
String cmd="pm enable " + packageName;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Toast.makeText(context, "解除冻结失败", 0).show();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Toast.makeText(context, "解除冻结成功", 0).show();
return true;
}
版权声明:本文标题:android 执行shell root权限,Android App 获取root权限后,执行shell命令 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1727080102a1096728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论