Power key event in android?

前端 未结 3 1881
鱼传尺愫
鱼传尺愫 2021-01-16 23:27

I want to listen to power key event. How can I do that?

Currently the code I am using is this:

    @Override
public boolean onKeyDown(int keyCode, Ke         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 00:01

    You should check in your override function onKeyDown

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_POWER) {
            // Do something here...
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    and the same with onKeyLongPress if you using

    Another way you would like to handle screen off, you can register BroadcastReceiver with action Intent.ACTION_SCREEN_OFF

提交回复
热议问题