Android Volume Key Event Capture

落爺英雄遲暮 提交于 2019-12-03 09:07:43
Varun Sridharan

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

As mentioned in Phonegap API Docs, This following code works only in Blackberry:

document.addEventListener("volumeupbutton", yourCallbackFunction, false);
document.addEventListener("volumedownbutton", yourCallbackFunction, false);

But there's nothing harm in checking it out. It might work with the latest Phonegap SDK

hope this helps!

You can add events listener in the PhoneGap for volume key buttons as described in the PhoneGap Documentation like this

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!