Android App 获取root权限后,执行shell命令

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">背景:</span>

做一个应用,需求是获取root权限后可以停用/启用某些应用,包括系统应用!停用的应用将不在设置-->应用中展示,大部分系统自带的桌面应该也不会再显示!

如果是用menifest.xml中添加android:sharedUserId="android.uid.system" ,需要包的签名与系统的签名一致!

所以考虑使用shell命令执行!

1.获取root权限:String cmd="chmod 777 " + pkgCodePath;

<pre name="code" class="java">/**
	 * 获取root权限
	 * @param pkgCodePath 通过getPackageCodePath() 获取
	 * @return
	 */
	public static boolean getRootPermission(String pkgCodePath){
		Process process = null;  
        DataOutputStream os = null;  
        try {  
            String cmd="chmod 777 " + pkgCodePath;  
            process = Runtime.getRuntime().exec("su"); //切换到root帐号  
            os = new DataOutputStream(process.getOutputStream());  
            os.writeBytes(cmd + "\n");  
            os.writeBytes("exit\n");  
            os.flush();  
            process.waitFor();  
        } catch (Exception e) {  
            return false;  
        } finally {  
            try {  
                if (os != null) {  
                    os.close();  
                }  
                process.destroy();  
            } catch (Exception e) {  
            }  
        }
        return true;  
	}

2.判断root权限获取结果:ls -l pkgCodePath

/**
	 * 判断某个路径的app的权限
	 * @param pkgCodePath
	 * @return
	 */
	private static boolean checkRoot(String pkgCodePath){
		Process process = null; 
		try {  
			String cmd="ls -l " + pkgCodePath;
            process = Runtime.getRuntime().exec(cmd); 
         
            BufferedReader mReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer mRespBuff = new StringBuffer();
            char[] buff = new char[1024*10];
            int ch = 0;
            while((ch = mReader.read(buff)) != -1){
            	mRespBuff.append(buff, 0, ch);
            }
            mReader.close();
            
            String regx="-rwxr[\\s\\S]+";//root后权限的表达式
 
            return <span style="font-family: Arial, Helvetica, sans-serif;">Pattern.matches(regx, mRespBuff.toString());</span>

        } catch (Exception e) {  
        	System.out.println("checkRoot exception");
            return false;  
        } finally {  
            try {  
                process.destroy();  
            } catch (Exception e) {  
            	e.printStackTrace();
            }  
        }
	}

3.停用应用:String cmd="pm disable " + packageName; 

/**
	 * 停用应用
	 * @param context 上下文信息
	 * @param packageName 应用的包名
	 * @return
	 */
	public static boolean invalidApp(Context context,String packageName){
		if(!getRootPermission(context.getPackageCodePath())){
			Toast.makeText(context, "冻结应用需要root权限", 0).show();
        	return false;
        }
		Process process = null;  
        DataOutputStream os = null;  
        try {  
            String cmd="pm disable " + packageName;  
            process = Runtime.getRuntime().exec("su"); //切换到root帐号  
            os = new DataOutputStream(process.getOutputStream());  
            os.writeBytes(cmd + "\n");  
            os.writeBytes("exit\n");  
            os.flush();  
            process.waitFor();  
        } catch (Exception e) { 
        	Toast.makeText(context, "冻结应用失败", 0).show();
            return false;  
        } finally {  
            try {  
                if (os != null) {  
                    os.close();  
                }  
                process.destroy();  
            } catch (Exception e) {  
            }  
        }  
        Toast.makeText(context, "冻结应用成功", 0).show();
        return true;  
	}

4.启用应用:String cmd="pm enable " + packageName;

/**
	 * 启用应用
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean validApp(Context context,String packageName){
		if(!getRootPermission(context.getPackageCodePath())){
			Toast.makeText(context, "解除冻结需要root权限", 0).show();
        	return false;
        }
		Process process = null;  
        DataOutputStream os = null;  
        try {  
            String cmd="pm enable " + packageName;  
            process = Runtime.getRuntime().exec("su"); //切换到root帐号  
            os = new DataOutputStream(process.getOutputStream());  
            os.writeBytes(cmd + "\n");  
            os.writeBytes("exit\n");  
            os.flush();  
            process.waitFor();  
        } catch (Exception e) {  
        	Toast.makeText(context, "解除冻结失败", 0).show();
            return false;  
        } finally {  
            try {  
                if (os != null) {  
                    os.close();  
                }  
                process.destroy();  
            } catch (Exception e) {  
            }  
        }  
        Toast.makeText(context, "解除冻结成功", 0).show();
        return true;  
	}




  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android应用中执行Shell命令可以通过`Runtime.getRuntime().exec(command)`方法来实现,其中`command`是要执行Shell命令字符串,比如`am start -n com.example.app/.MainActivity`。 要实现从一个Android应用中跳转到另一个应用,可以通过调用Shell命令执行`am start`命令。`am start`命令用于启动一个指定应用的特定Activity。 首先,需要确保设备已经root或者应用拥有相应的系统权限,才能执行Shell命令。然后,在应用中通过`Runtime.getRuntime().exec()`方法执行Shell命令。 下面是一个示例代码: ```java try { // 构建要执行命令 String packageName = "com.example.app"; String activityName = "com.example.app.MainActivity"; String command = "am start -n " + packageName + "/" + activityName; // 执行Shell命令 Process process = Runtime.getRuntime().exec(command); // 读取命令结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { Log.d("Shell", line); } // 等待命令执行完成 process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ``` 上述代码中,将要跳转的应用的包名和Activity名拼接成一个命令字符串,然后通过`Runtime.getRuntime().exec()`方法执行Shell命令。读取命令结果可以通过`InputStreamReader`和`BufferedReader`来实现,可以根据需要处理命令输出的结果。最后使用`process.waitFor()`等待命令执行完成。 需要注意的是,执行Shell命令需要小心处理,确保没有安全隐患。同时,要避免滥用Shell命令,以免影响到设备的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值