SDK Android : How to open Shutdown/Reboot dialog for the device

人走茶凉 提交于 2020-01-07 06:16:08

问题


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


来源:https://stackoverflow.com/questions/19930790/sdk-android-how-to-open-shutdown-reboot-dialog-for-the-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!