Java以超级用户身份执行系统命令的方式

执行Linux系统命令, 如果无法获取root权限, 会抛出异常

    public void testDate(){
        Log.i("zmr", "testDate");
        try {
            Process process = Runtime.getRuntime().exec("su");
            String datetime="20131023.112800"; //测试的设置的时间【时间格式 yyyyMMdd.HHmmss】
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("setprop persist.sys.timezone GMT\n");
            os.writeBytes("/system/bin/date -s "+datetime+"\n");
            os.writeBytes("clock -w\n");
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("zmr", "testDate e= " + e.getLocalizedMessage());
        }
    }

你可能感兴趣的:(Android)