android 执行shell root权限,Android App 获取root权限后,执行shell命令

2023-05-16

背景:

做一个应用,需求是获取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;

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 执行shell root权限,Android App 获取root权限后,执行shell命令 的相关文章

随机推荐