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

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

归属感 发布于 2017-03-11 字数 0 浏览 1204 回复 4

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归属感 2017-11-10 4 楼

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

public class ShellCommand {

private Boolean can_su;

public SH sh;
public SH su;

public ShellCommand() {
sh = new SH("sh");
su = new SH("su");
}

public boolean canSU() {
return canSU(false);
}

public boolean canSU(boolean force_check) {
if (can_su == null || force_check) {
CommandResult r = su.runWaitFor("id");
StringBuilder out = new StringBuilder();

if (r.stdout != null)
out.append(r.stdout).append(" ; ");
if (r.stderr != null)
out.append(r.stderr);

can_su = r.success();
}
return can_su;
}

public SH suOrSH() {
return canSU() ? su : sh;
}

public class CommandResult {
public final String stdout;
public final String stderr;
public final Integer exit_value;

CommandResult(Integer exit_value_in, String stdout_in, String stderr_in)
{
exit_value = exit_value_in;
stdout = stdout_in;
stderr = stderr_in;
}

CommandResult(Integer exit_value_in) {
this(exit_value_in, null, null);
}

public boolean success() {
return exit_value != null && exit_value == 0;
}
}

public class SH {
private String SHELL = "sh";

public SH(String SHELL_in) {
SHELL = SHELL_in;
}

public Process run(String s) {
Process process = null;
try {
process = Runtime.getRuntime().exec(SHELL);
DataOutputStream toProcess = new DataOutputStream(process.getOutputStream());
toProcess.writeBytes("exec " + s + "n");
toProcess.flush();
} catch(Exception e) {
process = null;
}
return process;
}

private String getStreamLines(InputStream is) {
String out = null;
StringBuffer buffer = null;
DataInputStream dis = new DataInputStream(is);

try {
if (dis.available() > 0) {
buffer = new StringBuffer(dis.readLine());
while(dis.available() > 0)
buffer.append("n").append(dis.readLine());
}
dis.close();
} catch (Exception ex) {
}
if (buffer != null)
out = buffer.toString();
return out;
}

public CommandResult runWaitFor(String s) {
Process process = run(s);
Integer exit_value = null;
String stdout = null;
String stderr = null;
if (process != null) {
try {
exit_value = process.waitFor();

stdout = getStreamLines(process.getInputStream());
stderr = getStreamLines(process.getErrorStream());

} catch(InterruptedException e) {
} catch(NullPointerException e) {
}
}
return new CommandResult(exit_value, stdout, stderr);
}
}
}

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

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

归属感 2017-10-30 3 楼

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

Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command+"n");
os.writeBytes("exitn");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());
return false;
}
finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
// nothing
}
}
return true;
}

}

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

虐人心 2017-09-19 2 楼

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("exitn");
//退出
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;
}

}

晚风撩人 2017-08-10 1 楼

public static boolean runRootCommand(String command) {

 Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command+"n");
os.writeBytes("exitn");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());
return false;
}
finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
// nothing
}
}
return true;
}

}