How to hook into the Power button in Android?

后端 未结 9 1665
挽巷
挽巷 2020-11-22 10:34

On an Android device, where the only buttons are the volume buttons and a power button, I want to make the app react to presses on the power button (long and short). How is

9条回答
  •  时光说笑
    2020-11-22 11:17

    Solution:

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
            Intent i = new Intent(this, ActivitySetupMenu.class);
            startActivity(i);
            return true;
        }
    
        return super.dispatchKeyEvent(event);
    }
    

提交回复
热议问题