简体   繁体   中英

How to detect power button event android app

I detect the power button pressed event in android. In the normal app pressed power button then the device screen is OFF, but when creating a system certificate app then the device screen remain ON.

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
    // do what you want with the power button
    return true;
}
return super.onKeyDown(keyCode, event);
}

or Try this

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        }
        return super.dispatchKeyEvent(event);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM