转自:http://www.oschina.net/code/snippet_816576_43883
弹框:
packagecom.linin.utils;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
/**
* root权限工具类
*
* @author linin
*
*/
publicclassRootUtil {
privatestaticfinal String TAG = "linin.root";
privatestaticboolean mHaveRoot = false;
/**
* 判断机器Android是否已经root,即是否获取root权限
*/
publicstaticboolean haveRoot() {
if(!mHaveRoot) {
intret = execRootCmdSilent("echo test");// 通过执行测试命令来检测
if(ret != -1) {
L.i(TAG,"have root!");
mHaveRoot = true;
}else{
L.i(TAG,"not root!");
}
}else{
L.i(TAG,"mHaveRoot = true, have root!");
}
returnmHaveRoot;
}
/** 获取root权限 */
publicstaticboolean root() {
try{
Runtime.getRuntime().exec(
newString[] { "/system/bin/su","-c",
"chmod 777 /dev/graphics/fb0" });
}catch(IOException e) {
e.printStackTrace();
L.i(TAG,"root fail!");
returnfalse;
}
L.i(TAG,"root success!");
returntrue;
}
/**
* 执行命令并且输出结果
*/
publicstaticString execRootCmd(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try{
Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
dos = newDataOutputStream(p.getOutputStream());
dis = newDataInputStream(p.getInputStream());
L.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while((line = dis.readLine()) != null) {
L.d("result", line);
result += line;
}
p.waitFor();
}catch(Exception e) {
e.printStackTrace();
}finally{
if(dos != null) {
try{
dos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(dis != null) {
try{
dis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
returnresult;
}
/**
* 执行命令但不关注结果输出
*/
publicstaticint execRootCmdSilent(String cmd) {
intresult = -1;
DataOutputStream dos = null;
try{
Process p = Runtime.getRuntime().exec("su");
dos = newDataOutputStream(p.getOutputStream());
L.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
}catch(Exception e) {
e.printStackTrace();
}finally{
if(dos != null) {
try{
dos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
returnresult;
}
}
不弹框:
public boolean isRootedByFile() {
boolean isRoot = false;
try {
if (isFileExists("/system/bin/su") || isFileExists("/system/xbin/su") ||
isFileExists("/system/app/superuser.apk") || isFileExists("/system/app/SuperSU.apk")) {
isRoot = true;
} else {
if(isFileExists("data/data/com.noshufou.android.su")) {
isRoot = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return isRoot;
}
public boolean isFileExists(String path) {
return new File(path).exists();
}
public static boolean isRootedByExec() {
boolean isRooted = false;
int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
if (ret != -1) {
isRooted = true;
}
return isRooted;
}
public static int execRootCmdSilent(String cmd) {
int result = -1;
DataOutputStream dos = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public boolean isRootedByFile() {
boolean isRoot = false;
try {
if (isFileExists("/system/bin/su") || isFileExists("/system/xbin/su") ||
isFileExists("/system/app/superuser.apk") || isFileExists("/system/app/SuperSU.apk")) {
isRoot = true;
} else {
if(isFileExists("data/data/com.noshufou.android.su")) {
isRoot = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return isRoot;
}
public boolean isFileExists(String path) {
return new File(path).exists();
}
不弹框:
//check rooted routine
BroadcastReceiver rootReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int result = intent.getIntExtra("Result", 0);
if (result == 1) {
mIsRooted = false;
} else {
mIsRooted = true;
}
unregisterReceiver(mRootReceiver);
}
};
registerReceiver(rootReceiver, new IntentFilter("com.sec.intent.action.SYSSCOPESTATUS"));
通过SYSSCOPESTATUS判断:(sysscope SYSTEM STATUS: Custom)
BroadcastReceiver mRootReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isRoot = (intent.getIntExtra("Result", 0) != 1);
}
};