package com.example.mylauncher; import java.io.DataOutputStream; import java.io.IOException; public class RootHelper { public static boolean executeRootCommand(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(); return process.waitFor() == 0; } catch (IOException | InterruptedException e) { e.printStackTrace(); return false; } finally { try { if (os != null) { os.close(); } if (process != null) { process.destroy(); } } catch (IOException e) { e.printStackTrace(); } } } public static boolean hasRoot() { return executeRootCommand("echo test"); } /** * Execute a shell command as root with a return value */ public static String executeRootCommandWithOutput(String command) { Process process = null; StringBuilder output = new StringBuilder(); try { process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); os.writeBytes(command + "\n"); os.writeBytes("exit\n"); os.flush(); java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line).append('\n'); } process.waitFor(); return output.toString().trim(); } catch (IOException | InterruptedException e) { e.printStackTrace(); return null; } finally { if (process != null) { process.destroy(); } } } /** * Check if a specific package is installed */ public static boolean isPackageInstalled(String packageName) { String command = "pm list packages " + packageName; String result = executeRootCommandWithOutput(command); return result != null && result.contains(packageName); } /** * Execute a specific app with root permissions */ public static boolean launchApp(String packageName) { String command = "monkey -p " + packageName + " -c android.intent.category.LAUNCHER 1"; return executeRootCommand(command); } /** * Reboot device */ public static void reboot() { executeRootCommand("reboot"); } /** * Power off device */ public static void powerOff() { executeRootCommand("reboot -p"); } /** * Lock screen */ public static void lockScreen() { executeRootCommand("input keyevent 26"); } /** * Change system settings (requires root) */ public static boolean changeSystemSetting(String setting, String value) { return executeRootCommand("settings put system " + setting + " " + value); } /** * Kill a specific app */ public static boolean killApp(String packageName) { return executeRootCommand("am force-stop " + packageName); } /** * Clear app data */ public static boolean clearAppData(String packageName) { return executeRootCommand("pm clear " + packageName); } }