筆者寫的 Reboot Control 是一個可以快速關機 / 重開機的小程式。但這程式並不是每一個行動裝置都能使用,至少該裝置一定要有 Root 過。 先來看要如何讓行動裝置關機: - 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 執行關機的指令。 若是要讓行動裝置重開機的話呢? - private void reboot(){
- try {
- Runtime.getRuntime().exec("su -c reboot");
- } catch (Exception ex) {
- e.printStackTrace();
- // 做其它處理或忽略它...
- }
- }
複製代碼 就這樣...是不是很簡單呢!
|