找回密碼
 註冊
搜索
查看: 1553|回復: 0

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

[複製鏈接]
發表於 2014-3-2 23:52:24 | 顯示全部樓層 |閱讀模式
 
Push to Facebook

筆者寫的 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. }
複製代碼
就這樣...是不是很簡單呢!


 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-5-1 04:08 , Processed in 0.021755 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表