javascript – PhoneGap事件volumeupbutton和volumedownbutton无法正常工作

前端之家收集整理的这篇文章主要介绍了javascript – PhoneGap事件volumeupbutton和volumedownbutton无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用PhoneGap Api 1.4.1并且我尝试使用1.5.0,PhoneGap事件volumeupbutton和volumedownbutton不工作,它既不适用于 Android设备也不适用于模拟器.当按下音量按钮时,它必须按下它显示警报查看代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
   <html>
    <head>
    <title>PhoneGap Volume Down Button Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Call onDeviceReady when PhoneGap is loaded.
    //
    // At this point,the document has loaded but phonegap.js has not.
    // When PhoneGap is loaded and talking with the native device,// it will call the event `deviceready`.
    //
    function onLoad() {
        document.addEventListener("deviceready",onDeviceReady,false);
    }

    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("volumedownbutton",onVolumeDownKeyDown,false);
        document.addEventListener("volumeupbutton",onVolumeUpKeyDown,false);
    }

    // Handle the volume down button
    //
    function onVolumeDownKeyDown() {
      alert("Down");
    }
    function onVolumeUpKeyDown() {
      alert("Up");
    }

      </script>
    </head>
    <body onload="onLoad()">
    </body>
    </html>

解决方法

您可以执行以下操作,以使用android运行音量按钮:
@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {

    //If volumedown 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;
}

@Override
public boolean onKeyUp(int keyCode,KeyEvent event) {
    LOG.d(TAG,"KeyUp has been triggered on the view" + keyCode);
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
        return true;
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
        return true;
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
        return true;
    }
    return false;
}

我从cordova错误报告中复制了这段代码.此代码适用于cordova 2.0.我瘦你必须将“cordova.fireDocumentEvent”更改为“phonegap.fireDocument”或“PhoneGap.fireDocumentEvent”

更新:
刚刚写了一篇关于这个bug的小博客帖子,这个帖子已经由上面的代码解决了.可以在该帖子中找到Cordova-Issue-Tracker的链接http://christian-kuetbach.de/blog/post/13

更新2:
问题似乎在cordova 1.9中得到修复:
https://issues.apache.org/jira/browse/CB-871

希望这可以帮助.

原文链接:https://www.f2er.com/js/150486.html

猜你在找的JavaScript相关文章