How to hook into the Power button in Android?

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

    you have to use this:

    BroadcastReceiver screenoff = new BroadcastReceiver() {
    
    public static final String Screenoff = "android.intent.action.SCREEN_OFF";
    
    @Override
    public void onReceive(Context context, Intent intent) {
            if (!intent.getAction().equals(Screenoff)) return;
            //put code to handle power press here
            return;
    
    }};
    

提交回复
热议问题