andoid手机Root的判断

package com.example.root;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if(hasRooted()){
                        Toast.makeText(getApplicationContext(), "isRoot OH yes!!!", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "isRoot OH no!!!", Toast.LENGTH_LONG).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if(isRoot()){
                        Toast.makeText(getApplicationContext(), "hasRooted OH yes, root!!!", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "hasRooted OH no, root!!!", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static boolean hasRooted() throws IOException, InterruptedException {
        Process process = null;
        DataOutputStream out = null;
        try {
            process = Runtime.getRuntime().exec("su");
            out = new DataOutputStream(process.getOutputStream());
            out.writeBytes("\n");
            out.writeBytes("exit\n");
            out.flush();
            process.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("Test",
                    "Unexpected error - Here is what I know: " + e.getMessage());
            return false;
        } finally {
            try {
                if (out != null)
                    out.close();
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    
    
    
    
    //方法2
    public static boolean isRoot() {
        boolean isRoot = false;
        String sys = 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);
        }
        ArrayList<String> res = run("/system/bin/sh", commands);
        String response = "";
        for (int i = 0; i < res.size(); i++) {
            response += res.get(i);
        }
        String root = "-rwsr-sr-x root     root";
        if (response.contains(root)) {
            isRoot = true;
        }

        return isRoot;

    }
    


    public static ArrayList<String> run(String shell, ArrayList<String> commands) {
        ArrayList<String> output = new ArrayList<String>();
        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) {
                Log.i(TAG, "command: " + command);
                shellInput.write((command + " 2>&1\n").getBytes());
            }

            shellInput.write("exit\n".getBytes());
            shellInput.flush();

            String line;
            while ((line = shellOutput.readLine()) != null) {
                Log.i(TAG, "result: " + line);
                output.add(line);
            }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            process.destroy();
        }

        return output;
    }
    
    
    
}

布局文件就不粘贴了,就两个按钮

你可能感兴趣的:(android,root)