您的位置:首页 > 移动开发 > Android开发

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

2016-11-28 15:53 399 查看
 近期正在做一些需要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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息