KEYCODE_POWER only dispatched on long press

为君一笑 提交于 2019-12-06 15:33:55
Paul

It takes more than that to capture the POWER button being pressed.

This is done on purpose by Google so that malicious applications cannot take over your device by preventing any user input and blocking hardware buttons. Besides that overriding the POWER button should be done only if there is a very good reason for that.

If you want to know how to do it then visit THIS question and check the top rated answer.

PS. Search next time.

ZeeShaN AbbAs

As mentioned here https://stackoverflow.com/a/15828732/1065357

if you override this method in activity or in view that has focus then user cannot long press power button

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(!hasFocus) {
        Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!