admin管理员组

文章数量:1392003

I am using PhoneGap Api 1.4.1 and also I tried with 1.5.0, The PhoneGap Event volumeupbutton and volumedownbutton is not working, neither it works on android device nor it works on emulator.when the volume button up or down is pressed it must display the alert see the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      ".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>

I am using PhoneGap Api 1.4.1 and also I tried with 1.5.0, The PhoneGap Event volumeupbutton and volumedownbutton is not working, neither it works on android device nor it works on emulator.when the volume button up or down is pressed it must display the alert see the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3/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>
Share Improve this question asked Mar 19, 2012 at 13:17 AsadYarKhanAsadYarKhan 6882 gold badges14 silver badges31 bronze badges 3
  • I also Tried this in iphone it is not working on it as well:( – AsadYarKhan Commented Mar 19, 2012 at 13:36
  • Wrong Question Not Supported in Iphone And Android :P – AsadYarKhan Commented Mar 19, 2012 at 14:52
  • What is a wrong question? You are free to delete your question or answer it yourself if you feel your question is unnecessary. – T. Junghans Commented Mar 20, 2012 at 11:38
Add a ment  | 

1 Answer 1

Reset to default 8 +50

You can do the following, to get the volume button running with 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;
}

I copied this code from a cordova bug report. This code is valid for cordova 2.0. I thin you'll have to change "cordova.fireDocumentEvent" to "phonegap.fireDocument" or "PhoneGap.fireDocumentEvent"

update: Just wrote a small blog-post about the bug, that was solved by the code above. The link to the Cordova-Issue-Tracker can be found in that post: http://christian-kuetbach.de/blog/post/13

update 2: The Issue seems to be fixed within cordova 1.9: https://issues.apache/jira/browse/CB-871

Hope this helps.

本文标签: javascriptPhoneGap Event volumeupbutton and volumedownbutton is not workingStack Overflow