Android 判断root权限的三种方法

2015-05-27 17:43:00
zengde
来源:
http://blog.csdn.net/zijie999/article/details/8186086
转贴 679
/**
 * 判断是否root权限
 */
package mobi.infolife.common.root;
import android.os.Build;
import java.io.*;
import java.util.ArrayList;
public class RootChecker {
	public static enum SHELL_CMD {
		check_su_binary(new String[] { "/s y s t e m/xbin/which", "su" });
		String[] command;
		SHELL_CMD(String[] command) {
			this.command = command;
		}
	}
	public RootChecker() {
	}
	public boolean checkRootMethod1() {
		String s = Build.TAGS;
		return s != null && s.contains("test-keys");
	}
	public boolean checkRootMethod2() {
		try {
			File file = new File("/s y s t e m/app/Superuser.apk");
			if (file.exists()) {
				return true;
			}
		} catch (Exception e) {
		}
		return false;
	}
	public boolean checkRootMethod3() {
		return e x e cuteCommand(SHELL_CMD.check_su_binary) != null;
	}
	public ArrayList<String> e x e cuteCommand(SHELL_CMD shellCmd) {
		String line = null;
		ArrayList<String> fullResponse = new ArrayList<String>();
		Process localProcess = null;
		try {
			localProcess = Runtime.getRuntime().e x e c(shellCmd.command);
		} catch (Exception e) {
			return null;
			// e.printStackTrace();
		}
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				localProcess.getOutputStream()));
		BufferedReader in = new BufferedReader(new InputStreamReader(
				localProcess.getInputStream()));
		try {
			while ((line = in.readLine()) != null) {
				// Log.d(LOG_TAG, "--> Line received: " + line);
				fullResponse.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Log.d(LOG_TAG, "--> Full response was: " + fullResponse);
		return fullResponse;
	}
	public boolean isDeviceRooted() {
		if (checkRootMethod1()) {
			return true;
		}
		if (checkRootMethod2()) {
			return true;
		}
		if (checkRootMethod3()) {
			return true;
		}
		return false;
	}
}