0

I'm new here.

I have a problem, i try to shutdown a 4.2.2 android device (not root).

I know that ACTION_SHUTDOWN not works with unroot device.

So i want to open the existing shutdown/reboot/airplane dialog, the same we get when we maintain the on/off button. Then the user just have to click shutdown button.

I try to create by this way, without result...

Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS); // or others settings         
startActivity(intent);

Thanks,

Imar
  • 13
  • 1
  • 3
  • Possible duplicate of [How to show power off menu on android on the click of a button?](https://stackoverflow.com/questions/40614913/how-to-show-power-off-menu-on-android-on-the-click-of-a-button) – fireb86 Feb 04 '19 at 16:26

2 Answers2

0
InputManager.getInstance().injectInputEvent(new InputEvent(KeyEvent.KEYCODE_POWER, keyCode), sync);

'sync' becomes either of these:

InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH 
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT

and you need

import android.hardware.input.InputManager;

This is untested, but puts you in the right direction, also bare in mind, functionality like this is NOT recommend.

failing that:

public static void simulateKey(final int KeyCode) {

    new Thread() {
        @Override
        public void run() {
            try {
                Instrumentation inst = new Instrumentation();
                inst.sendKeyDownUpSync(KeyCode);
            } catch (Exception e) {
                Log.e("Exception when sendKeyDownUpSync", e.toString());
            }
        }

    }.start();
}

and simply call it like

simulateKey(KeyEvent.KEYCODE_POWER);
Broak
  • 4,161
  • 4
  • 31
  • 54
  • 1
    injectInputEvent not works for my APK, i see that [link](http://stackoverflow.com/questions/5635486/android-keyevent-injection-requires-system-permissions). I try simulateKey(), no error, but no result, it isn't open shutdown dialog – Imar Nov 12 '13 at 15:14
0

The is no public sdk access to open the power button menu programatically.
This link has all the approches Here.Simulating power button press to display switch off dialog box

Community
  • 1
  • 1
SathMK
  • 1,171
  • 1
  • 8
  • 18