1、判断手机是否root
public static boolean isPhoneRoot() { if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists()) && (!new File("/system/sbin/su").exists()) && (!new File("/sbin/su").exists()) && (!new File("/vendor/bin/su").exists())){ // System.out.println("手机没有root"); return false; } else { // System.out.println("手机root了"); return true; } }
2、在手机root后获得root权限
/** * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) * * @return 应用程序是/否获取Root权限 */ public static boolean upgradeRootPermission(String pkgCodePath) { Process process = null; DataOutputStream os = null; try { if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists()) && (!new File("/system/sbin/su").exists()) && (!new File("/sbin/su").exists()) && (!new File("/vendor/bin/su").exists())){ return false; } String cmd="chmod 777 " + pkgCodePath; process = Runtime.getRuntime().exec("su"); //切换到root帐号 os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + " "); os.writeBytes("exit "); os.flush(); int res = process.waitFor(); // DataInputStream in = new DataInputStream(process.getErrorStream()); // if(res!=0||in!=null){ // String result = in.readLine(); // System.out.println("in.readLine(): "+result+" "+res); //// if("Permission denied".equals(in.readLine())){ // if(result!=null&&result.contains("denied")){ // return false; // } // }else{ //// System.out.println("in.readLine()==null"); // } if(res == 0){ return true; }else{ return false; } } catch (Exception e) { // System.out.println("Exception"); e.printStackTrace(); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); return false; } } }