灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2223回复:0

Android编程获取手机root权限问题

楼主#
更多 发布于:2012-09-06 13:47


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");去掉了,发现手机上提示获取权限成功,但是问题来了,手机黑屏,程序还在运行,就是黑屏。。。。。等下还会跳出是否强制关闭。。。这句话是啥意思呢????谢谢各位给点建议
求解释啊。。。。。。


喜欢0 评分0
游客

返回顶部