申请root权限
Posted All For Revenge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了申请root权限相关的知识,希望对你有一定的参考价值。
检测是否有root
public static boolean checkRooted() { boolean result = false; try { result = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists(); } catch (Exception e) { e.printStackTrace(); } return result; }
![](https://image.cha138.com/20210612/ead81824070f42dbafc93b882b079ce7.jpg)
1 public boolean getRootPermission(String pkgCodePath) { 2 Log.d(TAG, "pkgCodePath=" + pkgCodePath); 3 Process process = null; 4 DataOutputStream os = null; 5 try { 6 String cmd = "chmod 777 " + pkgCodePath; 7 process = Runtime.getRuntime().exec("su"); //切换到root帐号 8 os = new DataOutputStream(process.getOutputStream()); 9 os.writeBytes(cmd + "\\n"); 10 os.writeBytes("exit\\n"); 11 os.flush(); 12 process.waitFor(); 13 } catch (Exception e) { 14 Toast.makeText(this, "root error!" + pkgCodePath, Toast.LENGTH_SHORT).show(); 15 e.printStackTrace(); 16 return false; 17 } finally { 18 try { 19 if (os != null) { 20 os.close(); 21 } 22 process.destroy(); 23 24 25 } catch (Exception e) { 26 } 27 } 28 Toast.makeText(this, "root success!" + pkgCodePath, Toast.LENGTH_SHORT).show(); 29 return true; 30 }
然后在 想申请root的地方 执行方法
getRootPermission(getPackageCodePath());
静默安装 卸载
参考
http://www.2cto.com/kf/201503/381170.html
http://ju.outofmemory.cn/entry/108796
以上是关于申请root权限的主要内容,如果未能解决你的问题,请参考以下文章