Android when it started, really let some small number of software fire A, cut fruit, Tom Cat, blowing skirt is one of the representatives, of course, there is a strong practical shutdown software, we go to Baidu search.
Screenshots:
I. Understanding the CMD command
What we do under CMD, here's a few details. We just need to know the following commands.
Reboot: Su-c reboot
Shutdown: reboot-p
With this idea, we can go on to achieve
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= "reboot"/> <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=" Shutdown "/> <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=" Android:layout_height= "Wrap_content" android:layout_margintop= "15DP" android:background= "@drawable/btn_bg" android:text= " Exit "/> </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 ()) {//restart case r.id.btn_reboot://cmd command String cmd = "su-c reboot"; try {//Send request Runtime.getruntime (). exec (CMD); The catch (IOException e) {new Alertdialog.builder (mainactivity.this). Settitle ("I'm Sorry"). Setmessage ("Your phone is not root, can not achieve this function!")
"). Setpositivebutton (" OK ", null). Show ();
Break Shutdown case R.id.btn_power:try {//Get Administrator privileges su process process = Runtime.getruntime (). EXEC ("su");//input Command DataOutputStream
out = new DataOutputStream (Process.getoutputstream ());
Out.writebytes ("reboot-p\n");
End Out.writebytes ("exit\n");
Out.flush (); The catch (IOException e) {new Alertdialog.builder (mainactivity.this). Settitle ("I'm Sorry"). Setmessage ("Your phone is not root, can not achieve this function!")
"). Setpositivebutton (" OK ", null). Show ();
Break
Recovery case R.id.btn_recovery:try {//with shutdown principle 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 ("I'm Sorry"). Setmessage ("Your cell phone is not root, can not achieve this function!")
"). Setpositivebutton (" OK ", null). Show ();
Break
Exit case R.id.btn_finish:finish ();
Break }
}
}
What are you waiting for? Go ahead and try it, because we are directly getting the SU permission to send script commands, so we don't need additional permissions.
About Android under the cmd command shutdown restart and restart recovery related knowledge to everyone here, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!