user3388003
user3388003

Reputation: 33

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 ?

Upvotes: 1

Views: 1895

Answers (3)

Rodrigo Gontijo
Rodrigo Gontijo

Reputation: 587

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);

Upvotes: 0

Bjorn9000
Bjorn9000

Reputation: 108

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();
}

Upvotes: 1

Ankit
Ankit

Reputation: 256

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);

Upvotes: 1

Related Questions