SDK Android : How to open Shutdown/Reboot dialog f

2019-09-09 16:58发布

问题:

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,

回答1:

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



回答2:

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