public static boolean execRootCommand(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();
            Log.d(TAG, "--------------Root------------ ");
            // 1 is success
            if (process.waitFor() == 1) {
                Log.d(TAG, "Root SUCCESS ");
                return true;
            } else {
             Log.d(TAG, "Root error ");
                return false;
            }
        } catch (Exception e) {
            Log.d(TAG, "ROOT ERR:" + e.getMessage());
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
    }
    
    public static boolean chmodInput() {
        // exec "chmod 666 /dev/input/*"
        String command = "chmod 666 /dev/input/*";
        Log.d(TAG, "chmodInput!");
        return execRootCommand(command);
    }
    

解决方案 »

  1.   

    楼主没必要去判断waitFor的返回值,直接捕获异常就行。
       public static boolean execRootCommand(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();
                Log.d(TAG, "--------------Root------------ ");
                process.waitFor();
            } catch (Exception e) {
                Log.d(TAG, "ROOT ERROR:" + e.getMessage());
                return false;
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    process.destroy();
                    Log.d(TAG, "ROOT SUCCESS!");
                } catch (Exception e) {
                }
            }
            return true;
        }
        
        public static boolean chmodInput() {
            // exec "chmod 666 /dev/input/*"
            String command = "chmod 666 /dev/input/*";
            Log.d(TAG, "chmodInput!");
            return execRootCommand(command);
        }