当前位置: 移动技术网 > IT编程>移动开发>Android > Android下的CMD命令之关机重启及重启recovery

Android下的CMD命令之关机重启及重启recovery

2019年07月24日  | 移动技术网IT编程  | 我要评论

皖军同盟,脸上有痘疤怎么办,石家庄论坛

android刚兴起的时候,着实让一些小众软件火了一把,切水果,tom猫,吹裙子就是其中的代表,当然还有实用性很强的关机重启软件,我们去百度上搜索一下。

这里写图片描述

截图:

这里写图片描述

一.了解cmd 命令

我们在cmd下进行的操作什么的,这里就不一一细说了我们只要知道下面这几条命令就可以了

重启:su -c reboot

关机:reboot -p

有了这个思路,我们就可以去实现了

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="关机" />
<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;
}
}
}

还等什么?赶紧去试试吧吧,因为我们是直接取得su权限发送脚本命令,所以我们并不需要其他的权限.

关于android下的cmd命令之关机重启及重启recovery的相关知识就给大家介绍到这里,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网