0

I am getting a problem to switch off my mobile phone though programmitically in android.can you help me to find solution of this problem.

  • 1
    Possible duplicate of [Turn off device programmatically](https://stackoverflow.com/questions/24693682/turn-off-device-programmatically) – ALSP Oct 17 '19 at 03:33

1 Answers1

0

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

and put this permission on manifest:

<uses-permission android:name="android.permission.SHUTDOWN" />

Alternative solution: For Rooted device

Shutdown:

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

Restart:

Same code, just use reboot instead of reboot -p.

A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
  • This way is not working in my MI redmi 4 device, when I execute this code activity opens and close at the time. I checked all permission and Android Version but this is not working. – Mohit Varma Oct 17 '19 at 05:25
  • If you use first approach then you need access as a Super user. and If you go for 2nd solution, it should work fine. @MohitVarma – A S M Sayem Oct 17 '19 at 05:51
  • how can I get super user permission to do this task easily ? – Mohit Varma Oct 17 '19 at 06:50