Http://www.open-open.com/lib/view/open1409209890713.html
To realize the function of auto power off requires the root of the phone, if the phone has root privileges, when running the program, will be prompted that the application is requesting root permission, select Run to achieve shutdown. The code is as follows:
Activity
ImportJava.io.DataOutputStream;Importjava.io.IOException;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classAutomaticshutdownactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Button closebtn=(Button) Findviewbyid (r.id.main_btn_close); Closebtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {shutdown (); } }); } /*** The method of shutting down the machine requires the phone to have root authority*/ Private voidshutdown () {Try{Process Process= Runtime.getruntime (). EXEC ("su"); DataOutputStream out=NewDataOutputStream (Process.getoutputstream ()); Out.writebytes ("Reboot-p\n"); Out.writebytes ("Exit\n"); Out.flush (); } Catch(IOException e) {e.printstacktrace (); } } }
Android Auto Power off code