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

截圖:

一.了解CMD 命令
我們在cmd下進行的操作什么的,這里就不一一細說了我們只要知道下面這幾條命令就可以了
重啟:su -c reboot
關(guān)機:reboot -p
有了這個思路,我們就可以去實現(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)機" /> <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("你的手機未ROOT,無法實現(xiàn)該功能!")
.setPositiveButton("OK", null).show();
}
break;
// 關(guān)機
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("你的手機未ROOT,無法實現(xiàn)該功能!")
.setPositiveButton("OK", null).show();
}
break;
// recovery
case R.id.btn_recovery:
try {
// 同關(guān)機原理
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,無法實現(xiàn)該功能!")
.setPositiveButton("OK", null).show();
}
break;
// 退出
case R.id.btn_finish:
finish();
break;
}
}
}
還等什么?趕緊去試試吧吧,因為我們是直接取得su權(quán)限發(fā)送腳本命令,所以我們并不需要其他的權(quán)限.
關(guān)于Android下的CMD命令之關(guān)機重啟及重啟recovery的相關(guān)知識就給大家介紹到這里,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android實現(xiàn)短按電源鍵關(guān)機的實現(xiàn)代碼
- Android平臺預置GMS包后關(guān)機鬧鐘失效問題及解決方法
- Android實現(xiàn)關(guān)機后數(shù)據(jù)不會丟失問題
- Android 6.0開發(fā)實現(xiàn)關(guān)機菜單添加重啟按鈕的方法
- Android開發(fā)實現(xiàn)長按返回鍵彈出關(guān)機框功能
- Android仿蘋果關(guān)機界面實現(xiàn)代碼
- Android 修改系統(tǒng)關(guān)機動畫的實現(xiàn)
- Android實現(xiàn)關(guān)機與重啟的幾種方式(推薦)
- Android系統(tǒng)關(guān)機的全流程解析
- Android 實現(xiàn)關(guān)機的多種方式
相關(guān)文章
Android實現(xiàn)檢測手機多點觸摸點數(shù)
這篇文章主要為大家詳細介紹了Android實現(xiàn)檢測手機多點觸摸點數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android RecyclerView 實現(xiàn)快速滾動的示例代碼
本篇文章主要介紹了Android RecyclerView 實現(xiàn)快速滾動的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
android串口開發(fā)入門之搭建ndk開發(fā)環(huán)境及第一個jni調(diào)用程序
這篇文章主要給大家介紹了關(guān)于android串口開發(fā)入門之搭建ndk開發(fā)環(huán)境及第一個jni調(diào)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01
Android中加載網(wǎng)絡資源時的優(yōu)化可使用(線程+緩存)解決
Android 中加載網(wǎng)絡資源時的優(yōu)化;基本的思路是線程+緩存來解決,具體解決思路如下,有類似情況的朋友可以參考下哈2013-06-06

