is Phone got Rooted - Android Android OS

Android examples for Android OS:Root

Description

is Phone got Rooted

Demo Code


//package com.java2s;
import android.content.Context;
import android.util.Log;

import java.io.DataOutputStream;

import java.io.InputStream;

public class Main {
    public static boolean isRooted(Context mContext) {
        String apkRoot = "chmod 777 " + mContext.getPackageCodePath();
        return rootCommand(apkRoot);
    }//from   w w  w.  jav a 2 s  .  c o m

    public static boolean rootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            int exitcode = process.waitFor();

            InputStream is = process.getErrorStream();

            byte[] buffer = new byte[1024];
            int len = is.read(buffer);
            if (exitcode != 0) {
                Log.d("KindroidSecurity", "exitcode :" + exitcode);
                return false;
            }
            if (len > 0) {
                String ret = new String(buffer);
                Log.d("KindroidSecurity", "exitcode :" + exitcode + "; "
                        + ret);
                if (ret.contains("Permission denied")
                        || ret.contains("permission denied")
                        || ret.contains("not allowed to su")) {
                    return false;
                }
            }
        } catch (Exception e) {
            Log.d("DEBUG", "ROOT ERROR " + e.getMessage());
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }

                process.destroy();
            } catch (Exception e) {
            }
        }

        Log.d("DEBUG", "Root SUCCEEDED");
        return true;
    }
}

Related Tutorials