how to capture long press volume down key in android?

后端 未结 4 1215
暖寄归人
暖寄归人 2021-01-18 03:39

Just wondering whether anyone could tell me how to capture the long keypress of the volume down key in android.

Elaborated question:

I wanted to create a

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 04:44

    Note that to handle onKeyLongPress() you should track event onKeyDown(). and override both of them. please note the following examples.

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            event.startTracking();
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }
     @Override
    public boolean onKeyLongPress(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            //Do your stuff here
            return true;
        }
        return onKeyLongPress(keyCode,event);
    }
    

    Also note that onKeyDown by default returns false. So by triggering onKeyDown() volume won't be increased.

提交回复
热议问题