Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 CMD 指令實作 Android 關機、重啟與進入 Recovery

編輯:關於 Android 程式設計

Android 剛開始流行的時候,讓不少小眾 App 紅了一把——切水果、Tom 貓、吹裙子都是那個時代的代表。當然還有實用性更強的關機重啟工具,在網上搜一下就能看到不少。

一、先認識幾條指令

cmd 下的其他操作這裡不逐一細說,我們只需要記住兩條:

重啟:su -c reboot
關機:reboot -p

有了這個思路,接下來就能著手實作了。

activity_main.xml

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="clip_vertical" android:orientation="vertical" android:padding="15dp" > <Button android:id="@+id/btn_reboot" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/btn_bg" android:text="重啟" /> <Button android:id="@+id/btn_power" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/btn_bg" android:text="關機" /> <Button android:id="@+id/btn_recovery" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/btn_bg" android:text="recovery" /> <Button android:id="@+id/btn_finish" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/btn_bg" android:text="退出" /> </LinearLayout>

MainActivity

 package com.lgl.power; import java.io.DataOutputStream; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btn_reboot, btn_power, btn_recovery, btn_finish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_reboot = (Button) findViewById(R.id.btn_reboot); btn_reboot.setOnClickListener(this); btn_power = (Button) findViewById(R.id.btn_power); btn_power.setOnClickListener(this); btn_recovery = (Button) findViewById(R.id.btn_recovery); btn_recovery.setOnClickListener(this); btn_finish = (Button) findViewById(R.id.btn_finish); btn_finish.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { // 重啟 case R.id.btn_reboot: // cmd命令 String cmd = "su -c reboot"; try { // 發送請求 Runtime.getRuntime().exec(cmd); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉") .setMessage("你的手機未ROOT,無法實現該功能!") .setPositiveButton("OK", null).show(); } break; // 關機 case R.id.btn_power: try { // 獲取管理員權限su Process process = Runtime.getRuntime().exec("su"); // 輸入命令 DataOutputStream out = new DataOutputStream( process.getOutputStream()); out.writeBytes("reboot -p\n"); // 結束 out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉") .setMessage("你的手機未ROOT,無法實現該功能!") .setPositiveButton("OK", null).show(); } break; // recovery case R.id.btn_recovery: try { // 同關機原理 Process process = Runtime.getRuntime().exec("su"); DataOutputStream out = new DataOutputStream( process.getOutputStream()); out.writeBytes("reboot recovery\n"); out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉") .setMessage("你的手機未ROOT,無法實現該功能!") .setPositiveButton("OK", null).show(); } break; // 退出 case R.id.btn_finish: finish(); break; } } }

這裡有兩種呼叫 shell 的寫法值得比較。重啟那一段用的是 su -c reboot,把指令當參數直接交給 su 執行,一行搞定。關機與進入 recovery 則是先 exec("su") 開一個 root shell 進程,再透過 DataOutputStream 往它的標準輸入寫入指令——這種方式的好處是可以連續下多道指令而不用重複取得權限,代價是必須自己處理串流的收尾。

用 DataOutputStream 寫指令時,字串結尾一定要補上 \n,否則 shell 會一直等在輸入狀態,指令不會被執行;最後再補一道 exit 並 flush,讓進程正常結束、資源釋放。

另外提醒兩點。第一,這些功能都依賴 Root 權限,未 Root 的裝置只會走 catch 分支彈出提示,因此程式碼裡不需要另外宣告特殊權限。第二,Runtime.exec 不屬於 Android 官方公開 API,各家 ROM 對 su 的實作與 Location 也可能不同,因此這類寫法在部分機型上可能無效,正式產品中若要提供電源管理功能,更合適的做法是引導使用者透過系統設定操作。

以上就是 Android 下以 CMD 指令實作關機、重啟與進入 Recovery 的相關知識,希望對大家有所幫助。有疑問歡迎留言。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved