How to hook into the Power button in Android?

后端 未结 9 1542
挽巷
挽巷 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:09

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!hasFocus) {
           Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }
    

提交回复
热议问题