Android编程获取手机root权限问题
2481 点击·0 回帖
![]() | ![]() | |
![]() | public class DemoActivity extends Activity { public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授权root权限命令 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AlertDialog.Builder(this).setMessage(rootCommand(rootPowerCommand)+"....").show(); File []files = new File("/root").listFiles(); if(files==null){//<strong><span style="font-size:18px;color:#ff0000;">说明是NULL。。。。就是不能访问其下的文件了 </span></strong> new AlertDialog.Builder(this).setMessage(".OK...").show(); } // files[0].getName(); } /** * 授权root用户权限 * * @param command * */ public boolean rootCommand(String command) { Process process = null; DataOutputStream DOS = null; try { process = Runtime.getRuntime().exec("su"); DOS = new DataOutputStream(process.getOutputStream()); DOS.writeBytes(command + "\n"); DOS.writeBytes("exit\n"); DOS.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (DOS != null) { DOS.close(); } process.destroy(); } catch (Exception e) { } } return true; } } 最近再写一个文件管理器,需要访问到root权限才能访问的文件夹, 虽说获取权限成功,但是还是不能访问到需root权限才能访问的文件夹,同样的问题我同学写的一模一样的获取权限方法,他就可以,奇怪。。 我放到已经root过的手机上测试,没提示说要获取root,直接就报错了。。。。 今天改了一下,把DOS.writeBytes("exit\n");去掉了,发现手机上提示获取权限成功,但是问题来了,手机黑屏,程序还在运行,就是黑屏。。。。。等下还会跳出是否强制关闭。。。这句话是啥意思呢????谢谢各位给点建议 求解释啊。。。。。。 | |
![]() | ![]() |