1

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 ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143

3 Answers3

1

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);
Ankit
  • 256
  • 1
  • 9
  • I think this will work without rooting. Moreover it is only shuting down the phone, Who will turn it on ? – Lucifer Mar 11 '14 at 12:27
  • 1
    not working it require grandacccess permission from Super user when i allow it nothing perform – user3388003 Mar 11 '14 at 12:31
  • you have to use singing your apk with system key. Sorry I forgot to mention that. – Ankit Mar 11 '14 at 12:44
  • Use android/out/host//framework/signapk.jar to sign your app with below listed Files - platform.x509.pem - platform.pk8 Located in android/build/target/product/security. And then use bellow command:- java -jar signapk.jar platform.x509.pem platform.pk8 – Ankit Mar 12 '14 at 07:15
1

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();
}
Community
  • 1
  • 1
Bjorn9000
  • 98
  • 9
0

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);
Rodrigo Gontijo
  • 587
  • 1
  • 9
  • 21