Android root环境下的一些可用操作(关机命令,系统时间,重启命令)

 近期正在做一些需要root权限的功能,比如关机,重启等,下面总结一下用过的root吓得linux命令使用

 1.重启操作

Runtime.getRuntime().exec("su -c reboot");

   2.关机(1)

Runtime.getRuntime().exec("su -c reboot -p");

本来要用shutdown指令的,但是网上查的时候说android下没有这个文件,所以用-p的方式,但是我用这种执行方式测试的时候是重启,下面是另一种成功的执行方式

  3.关机(2)

createSuProcess("reboot -p").waitFor();
public static Process createSuProcess(String cmd) throws IOException {
        DataOutputStream os = null;
        Process process = createSuProcess();
        try {
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit $?\n");
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                }
            }
        }
        return process;
    }
public static Process createSuProcess() throws IOException  {
        File rootUser = new File("/system/xbin/ru");
        if(rootUser.exists()) {
            return Runtime.getRuntime().exec(rootUser.getAbsolutePath());
        } else {
            return Runtime.getRuntime().exec("su");
        }
    }
4.系统时间调节

try {
                Process process = Runtime.getRuntime().exec("su");
                String datetime=timeSu.replace(" ",".").replace(":","") + "00"; //测试的设置的时间【时间格式 yyyyMMdd.HHmmss】
                LogMessage.write(1,"时间设置参数:" + datetime);
                DataOutputStream os = new DataOutputStream(process.getOutputStream());
                os.writeBytes("setprop persist.sys.timezone GMT+16:00\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();
            }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值