Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery
Android剛興起的時(shí)候,著實(shí)讓一些小眾軟件火了一把,切水果,Tom貓,吹裙子就是其中的代表,當(dāng)然還有實(shí)用性很強(qiáng)的關(guān)機(jī)重啟軟件,我們?nèi)グ俣壬纤阉饕幌隆?/p>
截圖:
一.了解CMD 命令
我們在cmd下進(jìn)行的操作什么的,這里就不一一細(xì)說了我們只要知道下面這幾條命令就可以了
重啟:su -c reboot
關(guān)機(jī):reboot -p
有了這個(gè)思路,我們就可以去實(shí)現(xiàn)了
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://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="關(guān)機(jī)" /> <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 { // 發(fā)送請求 Runtime.getRuntime().exec(cmd); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉") .setMessage("你的手機(jī)未ROOT,無法實(shí)現(xiàn)該功能!") .setPositiveButton("OK", null).show(); } break; // 關(guān)機(jī) case R.id.btn_power: try { // 獲取管理員權(quán)限su Process process = Runtime.getRuntime().exec("su"); // 輸入命令 DataOutputStream out = new DataOutputStream( process.getOutputStream()); out.writeBytes("reboot -p\n"); // 結(jié)束 out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉") .setMessage("你的手機(jī)未ROOT,無法實(shí)現(xiàn)該功能!") .setPositiveButton("OK", null).show(); } break; // recovery case R.id.btn_recovery: try { // 同關(guān)機(jī)原理 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("你的手機(jī)未ROOT,無法實(shí)現(xiàn)該功能!") .setPositiveButton("OK", null).show(); } break; // 退出 case R.id.btn_finish: finish(); break; } } }
還等什么?趕緊去試試吧吧,因?yàn)槲覀兪侵苯尤〉胹u權(quán)限發(fā)送腳本命令,所以我們并不需要其他的權(quán)限.
關(guān)于Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery的相關(guān)知識(shí)就給大家介紹到這里,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
- Android平臺(tái)預(yù)置GMS包后關(guān)機(jī)鬧鐘失效問題及解決方法
- Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題
- Android 6.0開發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法
- Android開發(fā)實(shí)現(xiàn)長按返回鍵彈出關(guān)機(jī)框功能
- Android仿蘋果關(guān)機(jī)界面實(shí)現(xiàn)代碼
- Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫的實(shí)現(xiàn)
- Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
- Android系統(tǒng)關(guān)機(jī)的全流程解析
- Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式
相關(guān)文章
詳解Android廣播Broadcast的啟動(dòng)流程
這篇文章主要為大家介紹了Android廣播Broadcast啟動(dòng)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03以一個(gè)著色游戲展開講解Android中區(qū)域圖像填色的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)區(qū)域圖像顏色填充的方法,文中以一個(gè)著色游戲?yàn)槔v解了邊界的填充等各種填色操作,需要的朋友可以參考下2016-02-02使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android錄音時(shí)獲取分貝值的方法代碼實(shí)例
這篇文章主要介紹了Android錄音時(shí)獲取分貝值的方法代碼實(shí)例,本文直接給出核心實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04Android自定義水波紋動(dòng)畫Layout實(shí)例代碼
這篇文章主要介紹了Android自定義水波紋動(dòng)畫Layout的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信詳解
這篇文章主要給大家介紹了關(guān)于Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Android實(shí)現(xiàn)單項(xiàng)、多項(xiàng)選擇操作
這篇文章主要介紹了Android實(shí)現(xiàn)單項(xiàng)、多項(xiàng)選擇操作的相關(guān)資料,單項(xiàng)選擇、多項(xiàng)選擇操作在項(xiàng)目開發(fā)中經(jīng)常應(yīng)用,感興趣的小伙伴們可以參考一下2016-04-04Android實(shí)現(xiàn)滑動(dòng)到頂部懸停的效果
這篇文章給大家介紹一種不常見的實(shí)現(xiàn)Android滑動(dòng)到頂部懸停效果的方式,對大家開發(fā)Android具有一定的參考借鑒價(jià)值,有需要的朋友們可以來一起看看。2016-09-09Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03