2012-03-19 89 views
6

我使用PhoneGap Api 1.4.1,並且我嘗試使用1.5.0,PhoneGap Event volumeupbutton和volumedownbutton不起作用,它既不能在Android設備上工作,也不能在模擬器上工作。當音量按鈕向上或向下按下它必須顯示警報看到代碼:PhoneGap Event volumeupbutton和volumedownbutton不起作用

<!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> 
+0

我也試過這在iphone它不工作,以及:( – AsadYarKhan 2012-03-19 13:36:38

+0

錯誤的問題不支持在iPhone和Android:P – AsadYarKhan 2012-03-19 14:52:17

+0

什麼是錯誤的問題?你可以​​自由刪除你的問題或自己回答,如果你感覺你的問題是不必要的 – 2012-03-20 11:38:04

回答

8

你可以做到以下幾點,讓音量按鈕,運行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 2.0。我瘦你必須改變「cordova.fireDocumentEvent」到「phonegap.fireDocument」或「PhoneGap.fireDocumentEvent」

更新: 只是寫了一個小博客員額有關的bug,這是由解決上面的代碼。到科爾多瓦,問題跟蹤鏈接可以在該職位上找到:http://christian-kuetbach.de/blog/post/13

更新2: 這個問題似乎是固定科爾多瓦1.9中: https://issues.apache.org/jira/browse/CB-871

希望這有助於。

+0

你可以提供它作爲電話差距插件 – 2012-10-15 07:53:19

+0

我有cordova-1.8.1.js我需要這一個也 – 2012-10-15 07:54:51

+0

我認爲這會更容易,如果你覆蓋你的原生入門點(類,從DroidGap繼承下來),然後把它放在那裏我想我會在cordova項目中提交一個bug報告 – 2012-10-15 08:01:02

相關問題