感谢github前辈分享
参考来源:https://github.com/visi0nary/App2SystemMover/blob/master/app/src/main/java/de/visi0nary/app2system/MainActivity.java
package com.palmv.networksalebox.util;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
/**
* @author:czg *
* @date:2016/8/24
* @description:用户判断apk是否已经拥有root权限
*/
public class RootUtil {
static String TAG = "RootUtil";
static private Process suProcess = null;
static private DataOutputStream stdin = null;
static private BufferedWriter writer = null;
static private boolean rootInitialized =false; //app是否或许root权限
/**
* app获取root权限初始化
* @return 是否获取root权限
*/
static public boolean initializeRoot(){
int returnValue = -1;
rootInitialized = false;
if (DeviceisRoot()) { //DroidPackageUtil.isRoot()
try {
// start an SU process
suProcess = Runtime.getRuntime().exec("su");
stdin = new DataOutputStream(suProcess.getOutputStream());
writer = new BufferedWriter(new OutputStreamWriter(stdin));
//==
stdin.writeBytes("exit\n");
stdin.flush();
// wait for the SU process to terminate (if it exists)
suProcess.waitFor();
// check for the exit value (0 if it was a root process, 255 if not)
returnValue = suProcess.exitValue();
Log.i("Main", "结果"+ returnValue);
rootInitialized = (returnValue==0);
return rootInitialized;
}
catch (Exception e) {
Log.i(TAG,"判断程序是否有root权限发生异常" + e.toString());
} finally {
// if (suProcess != null && stdin != null && writer != null) {
// rootInitialized = true;
// }
}
}
else {
Log.i(TAG,"设置没有root过" );
rootInitialized = false;
}
return rootInitialized;
}
/**
* apk取消root
*/
static public void stopRoot() {
// stop root, clean up
if(rootInitialized) {
try {
writer.close();
stdin.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
suProcess.destroy();
}
}
}
/**
* 当前app 的root状态
* @return
*/
static public boolean isRootInitialized() {
return rootInitialized;
}
/**
* 手机或设备是否root过
*
* @return
*/
public static boolean DeviceisRoot() {
boolean isRoot = false;
String sys = java.lang.System.getenv("PATH");
ArrayList<String> commands = new ArrayList<String>();
String[] path = sys.split(":");
for (int i = 0; i < path.length; i++) {
String commod = "ls -l " + path[i] + "/su";
commands.add(commod);
java.lang.System.out.println("commod : " + commod);
}
ArrayList<String> res = run("/system/bin/sh", commands);
String response = "";
for (int i = 0; i < res.size(); i++) {
response += res.get(i);
}
int inavailableCount = 0;
String root = "-rwsr-sr-x root root";
for (int i = 0; i < res.size(); i++) {
if (res.get(i).contains("No such file or directory")
|| res.get(i).contains("Permission denied")) {
inavailableCount++;
}
}
return inavailableCount < res.size();
}
// 批量运行命令行
private static ArrayList run(String shell, ArrayList<String> commands) {
ArrayList output = new ArrayList();
Process process = null;
try {
process = Runtime.getRuntime().exec(shell);
BufferedOutputStream shellInput = new BufferedOutputStream(
process.getOutputStream());
BufferedReader shellOutput = new BufferedReader(
new InputStreamReader(process.getInputStream()));
for (String command : commands) {
shellInput.write((command + " 2>&1\n").getBytes());
}
shellInput.write("exit\n".getBytes());
shellInput.flush();
String line;
while ((line = shellOutput.readLine()) != null) {
output.add(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
process.destroy();
}
return output;
}
}