Turn off device programmatically












22















I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.



My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.



I do not have the shutdown command. I do have the reboot command.



reboot -p does not help. It simply freezes the device while remaining powered on.



The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:



PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());


I am open to all suggestions - hacky or otherwise. The version of Android is expected to remain at 4.2.2.





Intents



This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?



Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);


The most luck I had with this was to request a shutdown by Intent:



Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);




Shutdown Thread



That is a bit closer. Definitely interesting. Can you find an example of using it?



So far I have come up with this:



Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);

for (Method m : sdClass.getDeclaredMethods()) {
if (m.getName().matches("shutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("rebootOrShutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("beginShutdownSequence")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
}
}


shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:




java.lang.UnsatisfiedLinkError: Native method not found:
com.android.server.power.PowerManagerService.nativeShutdown:()V at
com.android.server.power.PowerManagerService.nativeShutdown(Native
Method) at
com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)
at
com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)
at
com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)




lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.










share|improve this question

























  • Will this help you ? stackoverflow.com/questions/10411650/…

    – hungr
    Jul 11 '14 at 8:54











  • No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

    – Knossos
    Jul 11 '14 at 8:56











  • please have a look on the answer from Manty and comments below it, seems there is shutdown command.

    – hungr
    Jul 11 '14 at 8:59











  • you need root access and you can follow the link posted by @hungr..

    – Govind
    Jul 11 '14 at 9:21











  • @Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

    – Knossos
    Jul 11 '14 at 9:36


















22















I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.



My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.



I do not have the shutdown command. I do have the reboot command.



reboot -p does not help. It simply freezes the device while remaining powered on.



The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:



PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());


I am open to all suggestions - hacky or otherwise. The version of Android is expected to remain at 4.2.2.





Intents



This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?



Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);


The most luck I had with this was to request a shutdown by Intent:



Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);




Shutdown Thread



That is a bit closer. Definitely interesting. Can you find an example of using it?



So far I have come up with this:



Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);

for (Method m : sdClass.getDeclaredMethods()) {
if (m.getName().matches("shutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("rebootOrShutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("beginShutdownSequence")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
}
}


shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:




java.lang.UnsatisfiedLinkError: Native method not found:
com.android.server.power.PowerManagerService.nativeShutdown:()V at
com.android.server.power.PowerManagerService.nativeShutdown(Native
Method) at
com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)
at
com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)
at
com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)




lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.










share|improve this question

























  • Will this help you ? stackoverflow.com/questions/10411650/…

    – hungr
    Jul 11 '14 at 8:54











  • No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

    – Knossos
    Jul 11 '14 at 8:56











  • please have a look on the answer from Manty and comments below it, seems there is shutdown command.

    – hungr
    Jul 11 '14 at 8:59











  • you need root access and you can follow the link posted by @hungr..

    – Govind
    Jul 11 '14 at 9:21











  • @Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

    – Knossos
    Jul 11 '14 at 9:36
















22












22








22


8






I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.



My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.



I do not have the shutdown command. I do have the reboot command.



reboot -p does not help. It simply freezes the device while remaining powered on.



The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:



PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());


I am open to all suggestions - hacky or otherwise. The version of Android is expected to remain at 4.2.2.





Intents



This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?



Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);


The most luck I had with this was to request a shutdown by Intent:



Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);




Shutdown Thread



That is a bit closer. Definitely interesting. Can you find an example of using it?



So far I have come up with this:



Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);

for (Method m : sdClass.getDeclaredMethods()) {
if (m.getName().matches("shutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("rebootOrShutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("beginShutdownSequence")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
}
}


shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:




java.lang.UnsatisfiedLinkError: Native method not found:
com.android.server.power.PowerManagerService.nativeShutdown:()V at
com.android.server.power.PowerManagerService.nativeShutdown(Native
Method) at
com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)
at
com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)
at
com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)




lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.










share|improve this question
















I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.



My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.



I do not have the shutdown command. I do have the reboot command.



reboot -p does not help. It simply freezes the device while remaining powered on.



The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:



PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());


I am open to all suggestions - hacky or otherwise. The version of Android is expected to remain at 4.2.2.





Intents



This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?



Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);


The most luck I had with this was to request a shutdown by Intent:



Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);




Shutdown Thread



That is a bit closer. Definitely interesting. Can you find an example of using it?



So far I have come up with this:



Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);

for (Method m : sdClass.getDeclaredMethods()) {
if (m.getName().matches("shutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("rebootOrShutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("beginShutdownSequence")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
}
}


shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:




java.lang.UnsatisfiedLinkError: Native method not found:
com.android.server.power.PowerManagerService.nativeShutdown:()V at
com.android.server.power.PowerManagerService.nativeShutdown(Native
Method) at
com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)
at
com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)
at
com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)




lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.







android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 '14 at 14:32







Knossos

















asked Jul 11 '14 at 8:49









KnossosKnossos

11.7k74173




11.7k74173













  • Will this help you ? stackoverflow.com/questions/10411650/…

    – hungr
    Jul 11 '14 at 8:54











  • No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

    – Knossos
    Jul 11 '14 at 8:56











  • please have a look on the answer from Manty and comments below it, seems there is shutdown command.

    – hungr
    Jul 11 '14 at 8:59











  • you need root access and you can follow the link posted by @hungr..

    – Govind
    Jul 11 '14 at 9:21











  • @Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

    – Knossos
    Jul 11 '14 at 9:36





















  • Will this help you ? stackoverflow.com/questions/10411650/…

    – hungr
    Jul 11 '14 at 8:54











  • No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

    – Knossos
    Jul 11 '14 at 8:56











  • please have a look on the answer from Manty and comments below it, seems there is shutdown command.

    – hungr
    Jul 11 '14 at 8:59











  • you need root access and you can follow the link posted by @hungr..

    – Govind
    Jul 11 '14 at 9:21











  • @Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

    – Knossos
    Jul 11 '14 at 9:36



















Will this help you ? stackoverflow.com/questions/10411650/…

– hungr
Jul 11 '14 at 8:54





Will this help you ? stackoverflow.com/questions/10411650/…

– hungr
Jul 11 '14 at 8:54













No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

– Knossos
Jul 11 '14 at 8:56





No. The shutdown command is missing. Reboot freezes the device. The Power Manager has no option for actually turning the device off. Only sleep and reboot.

– Knossos
Jul 11 '14 at 8:56













please have a look on the answer from Manty and comments below it, seems there is shutdown command.

– hungr
Jul 11 '14 at 8:59





please have a look on the answer from Manty and comments below it, seems there is shutdown command.

– hungr
Jul 11 '14 at 8:59













you need root access and you can follow the link posted by @hungr..

– Govind
Jul 11 '14 at 9:21





you need root access and you can follow the link posted by @hungr..

– Govind
Jul 11 '14 at 9:21













@Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

– Knossos
Jul 11 '14 at 9:36







@Manty: That answer uses "shutdown". As previously stated, I do not have that available to me. None of the results of that link work for me.

– Knossos
Jul 11 '14 at 9:36














5 Answers
5






active

oldest

votes


















17














In my case, I do not think it is possible to shut the device down how I would like to.



The closest that I managed to get to my target was using:



Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);


That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.



In any case, I hope that my testing will help others in their quest.






share|improve this answer
























  • is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

    – rupesh
    Sep 3 '15 at 7:09






  • 1





    You should really start a new question for that.

    – Knossos
    Sep 3 '15 at 8:37











  • @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

    – unforgettableid
    Jun 6 '16 at 0:42








  • 1





    @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

    – Knossos
    Jun 6 '16 at 9:30






  • 1





    this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

    – Taifun
    Nov 21 '16 at 16:21



















5














Runtime.getRuntime().exec(new String{ "su", "-c", "reboot -p" });


it works, just with rooted devices!!






share|improve this answer


























  • I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37



















5














It work for me on rooted device.
If your device is rooted then you can use below approach



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".






share|improve this answer



















  • 1





    I got java.io.IOException: Permission denied but my device is rooted, any ideas?

    – Ziv Kesten
    Mar 6 '18 at 7:21











  • I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37



















5














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


and put this permission on manifest:



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





share|improve this answer



















  • 1





    This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

    – theicfire
    Mar 22 '18 at 23:36











  • Yes, it do the work, but using the intent is a better code! Works in any android!

    – Rodrigo Gontijo
    Mar 23 '18 at 4:19





















0














An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:



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


Good luck!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24693682%2fturn-off-device-programmatically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    17














    In my case, I do not think it is possible to shut the device down how I would like to.



    The closest that I managed to get to my target was using:



    Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    i.putExtra("android.intent.extra.KEY_CONFIRM", true);
    startActivity(i);


    That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.



    In any case, I hope that my testing will help others in their quest.






    share|improve this answer
























    • is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

      – rupesh
      Sep 3 '15 at 7:09






    • 1





      You should really start a new question for that.

      – Knossos
      Sep 3 '15 at 8:37











    • @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

      – unforgettableid
      Jun 6 '16 at 0:42








    • 1





      @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

      – Knossos
      Jun 6 '16 at 9:30






    • 1





      this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

      – Taifun
      Nov 21 '16 at 16:21
















    17














    In my case, I do not think it is possible to shut the device down how I would like to.



    The closest that I managed to get to my target was using:



    Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    i.putExtra("android.intent.extra.KEY_CONFIRM", true);
    startActivity(i);


    That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.



    In any case, I hope that my testing will help others in their quest.






    share|improve this answer
























    • is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

      – rupesh
      Sep 3 '15 at 7:09






    • 1





      You should really start a new question for that.

      – Knossos
      Sep 3 '15 at 8:37











    • @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

      – unforgettableid
      Jun 6 '16 at 0:42








    • 1





      @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

      – Knossos
      Jun 6 '16 at 9:30






    • 1





      this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

      – Taifun
      Nov 21 '16 at 16:21














    17












    17








    17







    In my case, I do not think it is possible to shut the device down how I would like to.



    The closest that I managed to get to my target was using:



    Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    i.putExtra("android.intent.extra.KEY_CONFIRM", true);
    startActivity(i);


    That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.



    In any case, I hope that my testing will help others in their quest.






    share|improve this answer













    In my case, I do not think it is possible to shut the device down how I would like to.



    The closest that I managed to get to my target was using:



    Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    i.putExtra("android.intent.extra.KEY_CONFIRM", true);
    startActivity(i);


    That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.



    In any case, I hope that my testing will help others in their quest.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 11 '14 at 14:34









    KnossosKnossos

    11.7k74173




    11.7k74173













    • is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

      – rupesh
      Sep 3 '15 at 7:09






    • 1





      You should really start a new question for that.

      – Knossos
      Sep 3 '15 at 8:37











    • @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

      – unforgettableid
      Jun 6 '16 at 0:42








    • 1





      @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

      – Knossos
      Jun 6 '16 at 9:30






    • 1





      this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

      – Taifun
      Nov 21 '16 at 16:21



















    • is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

      – rupesh
      Sep 3 '15 at 7:09






    • 1





      You should really start a new question for that.

      – Knossos
      Sep 3 '15 at 8:37











    • @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

      – unforgettableid
      Jun 6 '16 at 0:42








    • 1





      @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

      – Knossos
      Jun 6 '16 at 9:30






    • 1





      this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

      – Taifun
      Nov 21 '16 at 16:21

















    is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

    – rupesh
    Sep 3 '15 at 7:09





    is there any way to prevent the device to shutdown or restart by pressing power button. Also i need to restrict the the device to silent mode.

    – rupesh
    Sep 3 '15 at 7:09




    1




    1





    You should really start a new question for that.

    – Knossos
    Sep 3 '15 at 8:37





    You should really start a new question for that.

    – Knossos
    Sep 3 '15 at 8:37













    @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

    – unforgettableid
    Jun 6 '16 at 0:42







    @rup35h: You have two questions. Google a bit. If you don't find answers, please post two new Stack Overflow questions. So that you have a better chance of getting upvotes and answers: 1. Explain why you want these things. 2. Be very careful about spelling and grammar.

    – unforgettableid
    Jun 6 '16 at 0:42






    1




    1





    @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

    – Knossos
    Jun 6 '16 at 9:30





    @unforgettableid Was it really necessary to poke a 2 year old question comment? I doubt he is still looking for an answer to those questions.

    – Knossos
    Jun 6 '16 at 9:30




    1




    1





    this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

    – Taifun
    Nov 21 '16 at 16:21





    this solution would require the permission android.permission.SHUTDOWN and that permission is only available for system apps, see also stackoverflow.com/a/14065879/1545993

    – Taifun
    Nov 21 '16 at 16:21













    5














    Runtime.getRuntime().exec(new String{ "su", "-c", "reboot -p" });


    it works, just with rooted devices!!






    share|improve this answer


























    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37
















    5














    Runtime.getRuntime().exec(new String{ "su", "-c", "reboot -p" });


    it works, just with rooted devices!!






    share|improve this answer


























    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37














    5












    5








    5







    Runtime.getRuntime().exec(new String{ "su", "-c", "reboot -p" });


    it works, just with rooted devices!!






    share|improve this answer















    Runtime.getRuntime().exec(new String{ "su", "-c", "reboot -p" });


    it works, just with rooted devices!!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 19 '15 at 9:35









    Halvor Holsten Strand

    15.2k145774




    15.2k145774










    answered Mar 19 '15 at 9:10









    PerestroicoPerestroico

    5111




    5111













    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37



















    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37

















    I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37





    I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37











    5














    It work for me on rooted device.
    If your device is rooted then you can use below approach



    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".






    share|improve this answer



















    • 1





      I got java.io.IOException: Permission denied but my device is rooted, any ideas?

      – Ziv Kesten
      Mar 6 '18 at 7:21











    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37
















    5














    It work for me on rooted device.
    If your device is rooted then you can use below approach



    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".






    share|improve this answer



















    • 1





      I got java.io.IOException: Permission denied but my device is rooted, any ideas?

      – Ziv Kesten
      Mar 6 '18 at 7:21











    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37














    5












    5








    5







    It work for me on rooted device.
    If your device is rooted then you can use below approach



    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".






    share|improve this answer













    It work for me on rooted device.
    If your device is rooted then you can use below approach



    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".







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 28 '16 at 17:53









    Yogesh RathiYogesh Rathi

    3,91943260




    3,91943260








    • 1





      I got java.io.IOException: Permission denied but my device is rooted, any ideas?

      – Ziv Kesten
      Mar 6 '18 at 7:21











    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37














    • 1





      I got java.io.IOException: Permission denied but my device is rooted, any ideas?

      – Ziv Kesten
      Mar 6 '18 at 7:21











    • I needed to do "su", "0", "reboot", "-p"

      – theicfire
      Mar 22 '18 at 23:37








    1




    1





    I got java.io.IOException: Permission denied but my device is rooted, any ideas?

    – Ziv Kesten
    Mar 6 '18 at 7:21





    I got java.io.IOException: Permission denied but my device is rooted, any ideas?

    – Ziv Kesten
    Mar 6 '18 at 7:21













    I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37





    I needed to do "su", "0", "reboot", "-p"

    – theicfire
    Mar 22 '18 at 23:37











    5














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


    and put this permission on manifest:



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





    share|improve this answer



















    • 1





      This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

      – theicfire
      Mar 22 '18 at 23:36











    • Yes, it do the work, but using the intent is a better code! Works in any android!

      – Rodrigo Gontijo
      Mar 23 '18 at 4:19


















    5














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


    and put this permission on manifest:



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





    share|improve this answer



















    • 1





      This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

      – theicfire
      Mar 22 '18 at 23:36











    • Yes, it do the work, but using the intent is a better code! Works in any android!

      – Rodrigo Gontijo
      Mar 23 '18 at 4:19
















    5












    5








    5







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


    and put this permission on manifest:



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





    share|improve this answer













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


    and put this permission on manifest:



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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 29 '17 at 17:53









    Rodrigo GontijoRodrigo Gontijo

    4641717




    4641717








    • 1





      This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

      – theicfire
      Mar 22 '18 at 23:36











    • Yes, it do the work, but using the intent is a better code! Works in any android!

      – Rodrigo Gontijo
      Mar 23 '18 at 4:19
















    • 1





      This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

      – theicfire
      Mar 22 '18 at 23:36











    • Yes, it do the work, but using the intent is a better code! Works in any android!

      – Rodrigo Gontijo
      Mar 23 '18 at 4:19










    1




    1





    This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

    – theicfire
    Mar 22 '18 at 23:36





    This required the app to be a system app for me. Instead running a reboot -p subprocess worked without that need.

    – theicfire
    Mar 22 '18 at 23:36













    Yes, it do the work, but using the intent is a better code! Works in any android!

    – Rodrigo Gontijo
    Mar 23 '18 at 4:19







    Yes, it do the work, but using the intent is a better code! Works in any android!

    – Rodrigo Gontijo
    Mar 23 '18 at 4:19













    0














    An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:



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


    Good luck!






    share|improve this answer




























      0














      An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:



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


      Good luck!






      share|improve this answer


























        0












        0








        0







        An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:



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


        Good luck!






        share|improve this answer













        An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:



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


        Good luck!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 11:43









        SilverSilver

        278




        278






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24693682%2fturn-off-device-programmatically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word