Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery
Android剛興起的時(shí)候,著實(shí)讓一些小眾軟件火了一把,切水果,Tom貓,吹裙子就是其中的代表,當(dāng)然還有實(shí)用性很強(qiáng)的關(guān)機(jī)重啟軟件,我們?nèi)グ俣壬纤阉饕幌隆?/p>

截圖:

一.了解CMD 命令
我們?cè)赾md下進(jìn)行的操作什么的,這里就不一一細(xì)說(shuō)了我們只要知道下面這幾條命令就可以了
重啟: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ā)送請(qǐng)求
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉")
.setMessage("你的手機(jī)未ROOT,無(wú)法實(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,無(wú)法實(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,無(wú)法實(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í)就給大家介紹到這里,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
- Android平臺(tái)預(yù)置GMS包后關(guān)機(jī)鬧鐘失效問(wèn)題及解決方法
- Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題
- Android 6.0開(kāi)發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法
- Android開(kāi)發(fā)實(shí)現(xiàn)長(zhǎng)按返回鍵彈出關(guān)機(jī)框功能
- Android仿蘋(píng)果關(guān)機(jī)界面實(shí)現(xiàn)代碼
- Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫(huà)的實(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實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android?Flutter制作一個(gè)修改組件屬性的動(dòng)畫(huà)
flutter為我們提供了一個(gè)AnimationController來(lái)對(duì)動(dòng)畫(huà)進(jìn)行詳盡的控制,不過(guò)直接是用AnimationController是比較復(fù)雜的,如果只是對(duì)一個(gè)widget的屬性進(jìn)行修改,可以做成動(dòng)畫(huà)嗎,本文就來(lái)探討一下2023-05-05
Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
本篇文章主要介紹了Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
android串口開(kāi)發(fā)入門(mén)之搭建ndk開(kāi)發(fā)環(huán)境及第一個(gè)jni調(diào)用程序
這篇文章主要給大家介紹了關(guān)于android串口開(kāi)發(fā)入門(mén)之搭建ndk開(kāi)發(fā)環(huán)境及第一個(gè)jni調(diào)用程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Flutter實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部導(dǎo)航欄的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android中加載網(wǎng)絡(luò)資源時(shí)的優(yōu)化可使用(線程+緩存)解決
Android 中加載網(wǎng)絡(luò)資源時(shí)的優(yōu)化;基本的思路是線程+緩存來(lái)解決,具體解決思路如下,有類(lèi)似情況的朋友可以參考下哈2013-06-06

