android中如何通过代码检测是否有root权限?

封装了一个类,代码如下:


  1. public class ShellCommand {
  2. private Boolean can_su;    
  3. public SH sh;
  4. public SH su;
  5. public ShellCommand() {
  6. sh = new SH("sh");
  7. su = new SH("su");
  8. }
  9. public boolean canSU() {
  10. return canSU(false);
  11. }
  12. public boolean canSU(boolean force_check) {
  13. if (can_su == null || force_check) {
  14. CommandResult r = su.runWaitFor("id");
  15. StringBuilder out = new StringBuilder();
  16. if (r.stdout != null)
  17. out.append(r.stdout).append(" ; ");
  18. if (r.stderr != null)
  19. out.append(r.stderr);
  20. can_su = r.success();
  21. }
  22. return can_su;
  23. }
  24. public SH suOrSH() {
  25. return canSU() ? su : sh;
  26. }
  27. public class CommandResult {
  28. public final String stdout;
  29. public final String stderr;
  30. public final Integer exit_value;
  31. CommandResult(Integer exit_value_in, String stdout_in, String stderr_in)
  32. {
  33. exit_value = exit_value_in;
  34. stdout = stdout_in;
  35. stderr = stderr_in;
  36. }
  37. CommandResult(Integer exit_value_in) {
  38. this(exit_value_in, null, null);
  39. }
  40. public boolean success() {
  41. return exit_value != null && exit_value == 0;
  42. }
  43. }
  44. public class SH {
  45. private String SHELL = "sh";
  46. public SH(String SHELL_in) {
  47. SHELL = SHELL_in;
  48. }
  49. public Process run(String s) {
  50. Process process = null;
  51. try {
  52. process = Runtime.getRuntime().exec(SHELL);
  53. DataOutputStream toProcess = new DataOutputStream(process.getOutputStream());
  54. toProcess.writeBytes("exec " + s + "\n");
  55. toProcess.flush();
  56. } catch(Exception e) {
  57. process = null;
  58. }
  59. return process;
  60. }
  61. private String getStreamLines(InputStream is) {
  62. String out = null;
  63. StringBuffer buffer = null;
  64. DataInputStream dis = new DataInputStream(is);
  65. try {
  66. if (dis.available() > 0) {
  67. buffer = new StringBuffer(dis.readLine());
  68. while(dis.available() > 0)
  69. buffer.append("\n").append(dis.readLine());
  70. }
  71. dis.close();
  72. } catch (Exception ex) {
  73. }
  74. if (buffer != null)
  75. out = buffer.toString();
  76. return out;
  77. }
  78. public CommandResult runWaitFor(String s) {
  79. Process process = run(s);
  80. Integer exit_value = null;
  81. String stdout = null;
  82. String stderr = null;
  83. if (process != null) {
  84. try {
  85. exit_value = process.waitFor();
  86. stdout = getStreamLines(process.getInputStream());
  87. stderr = getStreamLines(process.getErrorStream());
  88. } catch(InterruptedException e) {
  89. } catch(NullPointerException e) {
  90. }
  91. }
  92. return new CommandResult(exit_value, stdout, stderr);
  93. }
  94. }
  95. }

通过return new ShellCommand().canSU()即可获得。

注意这个方法最好放在异步中做,因为不同的机器检查时间不同。


1、最直接的判断有无root :判断是否有权限管理。如果已经root,则手机往往会有权限管理器,可用安装需要权限的软件。
2、只要随便写个具有“写入”权限的代码,在Android真机的系统目录里试试就知道,如果“写入”成功,说明具有root权限。

如果非要代码检测,下面代码可以试试

package com.pocketdigi;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;

public class RootActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataInputStream stream;
if(isRooted()){
try {
stream = Terminal("ping -c 2 www.pocketdigi.com");
//其实ping并不需要root权限 ,这里是ping 2次后才停止,所以启动后需要一点时间才会有显示
//你可以自己换成需要root权限的命令试试
String temp;
while((temp=stream.readLine())!=null){
System.out.println(temp);
//循环输出返回值
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public DataInputStream Terminal(String command) throws Exception
{
Process process = Runtime.getRuntime().exec("su");
//执行到这,Superuser会跳出来,选择是否允许获取最高权限
OutputStream outstream = process.getOutputStream();
DataOutputStream DOPS = new DataOutputStream(outstream);
InputStream instream = process.getInputStream();
DataInputStream DIPS = new DataInputStream(instream);
String temp = command + "\n";
//加回车
DOPS.writeBytes(temp);
//执行
DOPS.flush();
//刷新,确保都发送到outputstream
DOPS.writeBytes("exit\n");
//退出
DOPS.flush();
process.waitFor();
return DIPS;
}
public boolean isRooted() {
//检测是否ROOT过
DataInputStream stream;
boolean flag=false;
try {
stream = Terminal("ls /data/");
//目录哪都行,不一定要需要ROOT权限的
if(stream.readLine()!=null)flag=true;
//根据是否有返回来判断是否有root权限
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();

}

return flag;
}

}


使用代码倒真是没试过,不过可以依照linux特性去思考。linux系统下root权限可以使用su命令,android本身也是小型的linux系统,所以可以尝试下面代码试验下:`
public static boolean runRootCommand(String command) {


  1. Process process = null;  
  2. DataOutputStream os = null;  
  3. try {  
  4. process = Runtime.getRuntime().exec("su");  
  5. os = new DataOutputStream(process.getOutputStream());  
  6. os.writeBytes(command+"\n");  
  7. os.writeBytes("exit\n");  
  8. os.flush();  
  9. process.waitFor();  
  10. } catch (Exception e) {  
  11. Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());  
  12. return false;  
  13. }  
  14. finally {  
  15. try {  
  16. if (os != null) {  
  17. os.close();  
  18. }  
  19. process.destroy();  
  20. } catch (Exception e) {  
  21. // nothing  
  22. }  
  23. }  
  24. return true;  
  25. }  

}
`
另外如果不使用代码检测,可以有一个方法,将android设备连接USB调试,待识别设备后输入adb shell,如果出现$表述无root权限,如果出现#则表示有root权限,希望对你有用。


public static boolean runRootCommand(String command) {


  1. Process process = null;  
  2. DataOutputStream os = null;  
  3. try {  
  4. process = Runtime.getRuntime().exec("su");  
  5. os = new DataOutputStream(process.getOutputStream());  
  6. os.writeBytes(command+"\n");  
  7. os.writeBytes("exit\n");  
  8. os.flush();  
  9. process.waitFor();  
  10. } catch (Exception e) {  
  11. Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());  
  12. return false;  
  13. }  
  14. finally {  
  15. try {  
  16. if (os != null) {  
  17. os.close();  
  18. }  
  19. process.destroy();  
  20. } catch (Exception e) {  
  21. // nothing  
  22. }  
  23. }  
  24. return true;  
  25. }  

}


你可能感兴趣的:(Android基础篇)