Home » Android 程式片段

[Android]透過程式讓行動裝置關機 / 重開機

筆者寫的 Reboot Control 是一個可以快速關機 / 重開機的小程式。但這程式並不是每一個行動裝置都能使用,至少該裝置一定要有 Root 過。

先來看要如何讓行動裝置關機:

1
2
3
4
5
6
7
8
9
10
11
12
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 (Exception e) {
		e.printStackTrace();
		// 做其它處理或忽略它...
	}
}

這邊是先取得 Runtime 物件後,再利用其 exec() 來執行 su 執行關機的指令。

若是要讓行動裝置重開機的話呢?

1
2
3
4
5
6
7
8
private void reboot(){
	try {
		Runtime.getRuntime().exec("su -c reboot");
	} catch (Exception ex) {
		e.printStackTrace();
		// 做其它處理或忽略它...
	}
}

就這樣...是不是很簡單呢!

1 筆針對 [Android]透過程式讓行動裝置關機 / 重開機 的迴響

發表迴響