Android Volume Key Event Capture

后端 未结 3 2102
清歌不尽
清歌不尽 2021-02-11 04:36

I am currently developing an app for android..

how i can capture the volume key event when user press in android phone

i am using

3条回答
  •  悲&欢浪女
    2021-02-11 05:05

    Use the below code to get the key event

    Use this code in your existing java class

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
        //If volume down key
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
            return true;
        } else {
            //return super.onKeyDown(keyCode, event); 
        }
        //return super.onKeyDown(keyCode, event);
    
        return true;
    }
    

    and using this below in your html page

    document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);  
    
    document.addEventListener("volumedownbutton", volumedownbutton, false);
    
    function    onVolumeUpKeyDown()
    {
        alert("Working Up")     
    }
    
    function    volumedownbutton()
    {
        alert("Working Down")       
    }
    

    I Got This Answer From Here https://stackoverflow.com/questions/9770901/phonegap-event-volumeupbutton-and-volumedownbutton-is-not-working

提交回复
热议问题