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

时间:2012-03-19 13:17:23

标签: javascript android cordova phonegap-plugins

我正在使用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>

1 个答案:

答案 0 :(得分:9)

您可以执行以下操作,以使用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

希望这有帮助。