2013年6月7日 星期五

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



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


筆者寫的 Reboot Control 是一個可以快速關機 / 重開機的小程式。但這程式並不是每一個行動裝置都能使用,至少該裝置一定要有 Root 過。
先來看要如何讓行動裝置關機:
檢視原始碼 Android
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("reboot recovery\n");
  out.writeBytes("exit\n");
  out.flush();
 } catch (Exception e) {
  e.printStackTrace();
  // 做其它處理或忽略它...
 }
}
這邊是先取得 Runtime 物件後,再利用其 exec() 來執行 su 執行關機的指令。
若是要讓行動裝置重開機的話呢?
檢視原始碼 Android
1
2
3
4
5
6
7
8
private void reboot(){
 try {
  Runtime.getRuntime().exec("su -c reboot");
 } catch (Exception ex) {
  e.printStackTrace();
  // 做其它處理或忽略它...
 }
}
就這樣...是不是很簡單呢!

------------------------------
hot reboot
try {
Process proc = Runtime.getRuntime()
            .exec(new String[]{ "su", "-c", "busybox killall system_server"});
    proc.waitFor();
} catch (Exception ex) {
    ex.printStackTrace();
}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...