简体   繁体   中英

How to power off programmatically a rooted phone ?

I want to power of my device programically my phone is rooted but this command not power of my device what is wrong ?

            button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
               Process chperm;
                try {
                    chperm=Runtime.getRuntime().exec("su");
                      DataOutputStream os = 
                          new DataOutputStream(chperm.getOutputStream());

                          os.writeBytes("reboot -p\n");
                          os.flush();



                         chperm.waitFor();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                    catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }   
        }
    });

How do I power off device programtically ?

Hope it will work for you.

Intent shutdown = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    shutdown.putExtra("android.intent.extra.KEY_CONFIRM", false);
    shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    thisActivity.startActivity(shutdown);

this is actually an answer from stackoverflow it self in a different thread. Programmatically switching off Android phone

try {
    Process proc = Runtime.getRuntime()
                    .exec(new String[]{ "su", "-c", "reboot -p" });
    proc.waitFor();
} catch (Exception ex) {
    ex.printStackTrace();
}

To use this code, you need Super User! Works on 4.0 and above!

 Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
            i.putExtra("android.intent.extra.KEY_CONFIRM", false);
            i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM