Android利用root权限开关机、休眠和唤醒

    在android的设备中如果我们想重启手机或者关机或,一般是需要在源码的环境下编译apk,并赋予其相应地系统权限,而如果想唤醒设备则需要wakelack。源码编译APP还是比较麻烦的,不过由于android的内核属于linux,那么在获取root权限的android设备商自然可以使用linux的开关机,唤醒休眠命令。

    (1)重启设备

	public void restart() {	  
		try {
			Process process = Runtime.getRuntime().exec("su");
			DataOutputStream out = new DataOutputStream(
					process.getOutputStream());
			out.writeBytes("reboot \n");
			out.writeBytes("exit\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
首先获取root权限,然后直接调用linux的reboot,android设备会立即重启。

    (2)休眠设备

	public void hibernate(){
		try {
			Process process = Runtime.getRuntime().exec("su");
			DataOutputStream out = new DataOutputStream(
					process.getOutputStream());
			out.writeBytes("echo mem > /sys/power/state \n");
			out.writeBytes("exit\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    (3)唤醒设备

	public void wakeup(){
		try {
			Process process = Runtime.getRuntime().exec("su");
			DataOutputStream out = new DataOutputStream(
					process.getOutputStream());
			out.writeBytes("echo on > /sys/power/state \n");
			out.writeBytes("exit\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
		KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
		keyguardLock.disableKeyguard();
	}
    (4)设备关机

	private void shutdown() {
		try {
			Process process = Runtime.getRuntime().exec("su");
			DataOutputStream out = new DataOutputStream(
					process.getOutputStream());
			out.writeBytes("reboot -p\n");
			out.writeBytes("exit\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}






你可能感兴趣的:(Android利用root权限开关机、休眠和唤醒)