Android Auto Power off code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.