root工具类
来源:互联网 发布:建筑力学分析软件 编辑:程序博客网 时间:2024/05/22 01:45
感谢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; }}
0 0
- root工具类
- 获取root权限工具类
- nexus 7 Root工具
- 手机root 工具
- 联想 S920 小白式一键ROOT工具
- 安卓ROOT工具汇总
- 华为3C刷机ROOT 一键ROOT必备工具
- TCL智能电视ROOT教程 附ROOT工具下载
- Struts2将对象放置在Root栈中和压入栈顶工具类
- Android程序的安全系统 root 工具
- Linux本地自动获取root权限工具
- Linux本地自动获取root权限工具
- 360root工具 v5.1.8 电脑版
- ftp工具root登入linux设置
- Android开发工具篇:华为手机root
- 能攻破所有安卓设备 最强Root工具诞生 万能一键Root
- ROOT
- ROOT
- Apache ZooKeeper
- 提高数据库查询效率的有效方法
- [转]hls之m3u8、ts、h264、AAC流格式详解
- Eclipse 配置 tomcat
- java反射要点
- root工具类
- Ubuntu14.04下安装google chrome浏览器
- PHP 字符串二进制相互转换
- CAS-比较&交换并发处理策略
- Codeforces Round #362 (Div. 2) B - Puzzles (树上概率dp)
- Cg(C for Graphic)标准函数库之数学函数与几何函数
- JAVA之类加载
- 细说JVM系列:JVM介绍
- va_start va_end