2

I am finding out a way to switch off my tablet automatically.

Right now when we long press power button, I get a confirmation for shutdown (Cancel or Ok). Is there a way to programmatically switch off the device without confirmation ?

Is this possible?

Adarsh H S
  • 1,208
  • 6
  • 21
  • 42

3 Answers3

3

No. Suitably rooted phones/tablets often have access to su/reboot commands, but for an off-the-shelf, commercially available device, no: there is no way to programatically shut it down.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • 2
    Taken from here http://stackoverflow.com/questions/6305491/power-off-or-restart-an-android-device-via-code-program – Antrromet Oct 03 '12 at 04:59
  • Thanks for the quick reply. In that case is there any listener to detect pop ups or dialog windows so that I can trigger OK button? – Adarsh H S Oct 03 '12 at 05:19
  • 1
    AFAIK You cannot determine if a dialog is shown. You can determine only if you are generating the dialog in your code, but in your case you need to determine if there is a dialog generated by the system. I searched a lot but couldnt find anything too. – Antrromet Oct 03 '12 at 05:41
2

This is a dicey one! As an app, you cant do much, but there is one way you can try this. Get a phone which is rooted and grants your application SuperUser permissions. Then you could try to run this piece of code from your APK.

Process mProcess = null;
DataOutputStream osStream;

try {
        mProcess = Runtime.getRuntime().exec("su");
    } catch (IOException e) {
        Log.e("Error","Unable to get SU permissions, quitting");
    }
    osStream = new DataOutputStream(mProcess.getOutputStream());
try {
        osStream.writeBytes("reboot");
        Thread.sleep(1000);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

Not a tested piece of code, but hopefully should give you some idea!

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
0

Although you can't really invoke the shutdown programmatically in non-rooted device, there's a way how to disable the confirmation dialog that occurs when you long-press the power button. There's a secret code

*#*#7594#*#* or *#7594#

which changes the power button behaviour - enables direct power off once the code enabled. You need to choose this code via default dialpad. Works on most Android phones.

Here's the list of some other secret codes.

Micer
  • 8,731
  • 3
  • 79
  • 73