PhoneGap的事件volumeupbutton和volumedownbutton不工作 [英] PhoneGap Event volumeupbutton and volumedownbutton is not working

查看:449
本文介绍了PhoneGap的事件volumeupbutton和volumedownbutton不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap的阿比1.4.1和我也试图与1.5.0,该PhoneGap的事件volumeupbutton和volumedownbutton不工作,没有它适用于Android设备,也没有它的工作原理上emulator.when音量键上或下pressed它必须显示警报见code:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // EN
                      http://www.w3.org/TR/html4/strict.dtd">
   < HTML>
    < HEAD>
    <冠军> PhoneGap的音量键例< /标题>

    <脚本类型=文/ JavaScript的字符集=utf-8SRC =phonegap.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的字符集=utf-8>

    //调用onDeviceReady当PhoneGap的加载。
    //
    //此时,文档加载但phonegap.js没有。
    //当PhoneGap的加载,并与本地的设备说话,
    //它会调用事件'deviceready`。
    //
    传播的onLoad(){
        document.addEventListener(deviceready,onDeviceReady,假);
    }

    // PhoneGap的是加载,这是现在可以安全地拨打电话PhoneGap的方法
    //
    传播onDeviceReady(){
        //注册事件监听器
        document.addEventListener(volumedownbutton,onVolumeDownKeyDown,假);
        document.addEventListener(volumeupbutton,onVolumeUpKeyDown,假);
    }

    //处理音量键
    //
    功能onVolumeDownKeyDown(){
      警报(下);
    }
    功能onVolumeUpKeyDown(){
      警报(上);
    }

      < / SCRIPT>
    < /头>
    <身体的onload =的onLoad()>
    < /身体GT;
    < / HTML>
 

解决方案

您可以做到以下几点,让音量按钮与Android运行:

  @覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){

    //如果volumedown关键
    如果(键code == KeyEvent.KEY code_VOLUME_DOWN){
        this.loadUrl(JavaScript的:cordova.fireDocumentEvent('volumedownbutton'););
        返回true;
    }否则,如果(键code == KeyEvent.KEY code_VOLUME_UP){
        this.loadUrl(JavaScript的:cordova.fireDocumentEvent('volumeupbutton'););
        返回true;
    } 其他 {
        //返回super.onKeyDown(键code,事件);
    }
    //返回super.onKeyDown(键code,事件);

    返回true;
}

@覆盖
公共布尔的onkeyup(INT键code,KeyEvent的事件){
    LOG.d(TAG的KeyUp已被触发的看法+键code);
    //如果返回键
    如果(键code == KeyEvent.KEY code_BACK){
        this.loadUrl(JavaScript的:cordova.fireDocumentEvent('后退按钮'););
        返回true;
    }
    // 遗产
    否则,如果(键code == KeyEvent.KEY code_MENU){
        this.loadUrl(JavaScript的:cordova.fireDocumentEvent('菜单按钮'););
        返回true;
    }
    //如果搜索关键字
    否则,如果(键code == KeyEvent.KEY code_SEARCH){
        this.loadUrl(JavaScript的:cordova.fireDocumentEvent('searchbutton'););
        返回true;
    }
    返回false;
}
 

我从科尔多瓦bug报告复制此code。这code是有效的科尔多瓦2.0。我瘦,你就必须改变cordova.fireDocumentEvent到phonegap.fireDocument或PhoneGap.fireDocumentEvent

更新: 刚写了一个小博客,张贴有关的错误,这是解决由code以上。链接到科尔多瓦,问题跟踪器可以在该职位中找到: http://christian-kuetbach.de/blog/交/ 13

更新2: 这个问题似乎是在科尔多瓦1.9固定: <一href="https://issues.apache.org/jira/browse/CB-871">https://issues.apache.org/jira/browse/CB-871

希望这有助于。

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.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>

解决方案

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.org/jira/browse/CB-871

Hope this helps.

这篇关于PhoneGap的事件volumeupbutton和volumedownbutton不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆