root工具类

感谢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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值