TShopping

標題: Android 透過程式讓行動裝置關機 / 重開機 [打印本頁]

作者: woff    時間: 2014-3-2 23:52
標題: Android 透過程式讓行動裝置關機 / 重開機

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

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

  1. private void shutdown() {
  2.         try {
  3.                 Process process = Runtime.getRuntime().exec("su");
  4.                 DataOutputStream out = new DataOutputStream(process.getOutputStream());
  5.                 out.writeBytes("reboot -p\n");
  6.                 out.writeBytes("exit\n");
  7.                 out.flush();
  8.         } catch (Exception e) {
  9.                 e.printStackTrace();
  10.                 // 做其它處理或忽略它...
  11.         }
  12. }
複製代碼

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

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

  1. private void reboot(){
  2.         try {
  3.                 Runtime.getRuntime().exec("su -c reboot");
  4.         } catch (Exception ex) {
  5.                 e.printStackTrace();
  6.                 // 做其它處理或忽略它...
  7.         }
  8. }
複製代碼
就這樣...是不是很簡單呢!







歡迎光臨 TShopping (http://www.tshopping.com.tw/) Powered by Discuz! X3.2