我正在为Android 4.4设备编写cordova(版本6)应用程序。在这里,我试图抓住按钮按下事件,例如音量降低按钮。不幸的是,我需要在服务器上托管应用程序。意味着远程加载所有html,css和js文件。而app的index.html
只包含:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>My App</title>
<script type="text/javascript">
function init() {
window.location.href="http://example.com:3000/myapp";
}
</script>
</head>
<body id="body" onload="init();">
</body>
</html>
在服务器端,cordova-js成功注入:
<script type="text/javascript" src="/assets/cordova.js"></script>
我自己构建了cordova-js,使用此存储库及其文档:https://github.com/apache/cordova-js
大多数服务器端cordova代码运行正常!例如。我添加了一些所有工作的插件(wifi信息,show-toast-messages等等)。但不幸的是, menu , back , volup , voldown 的按钮监听器不再工作了!他们曾经工作过,当cordova代码直接加载到设备上时,但由于我放在远程服务器上,它不再工作了。
来自服务器的JS文件:
//Gets called from the body-tag of the html-site - works!
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
//This works!
console.log("onDeviceReady");
//This doesn't do anything (but also no error messages)
navigator.app.overrideButton("backbutton", true);
navigator.app.overrideButton("menubutton", true);
document.addEventListener("backbutton", onBackKeyDown, false);
document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);
document.addEventListener("menubutton", onMenuKeyDown, false);
//Plugins that are loaded from here all work!
}
function onMenuKeyDown(event) {
//This doesn't work
console.log("menu pressed");
}
function onBackKeyDown(event) {
//This doesn't work
console.log("back pressed");
}
function onVolumeUpKeyDown() {
//This doesn't work
console.log("Volume up pressed");
}
有人可以告诉我,为什么这段代码不再有效并帮我解决这个问题?
答案 0 :(得分:0)
试试这个......
//Gets called from the body-tag of the html-site - works!
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
//This works!
console.log("onDeviceReady");
}
document.addEventListener("backbutton", onBackKeyDown, false);
document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);
document.addEventListener("menubutton", onMenuKeyDown, false);
function onMenuKeyDown(event) {
console.log("menu pressed");
}
function onBackKeyDown(event) {
console.log("back pressed");
}
function onVolumeUpKeyDown() {
console.log("Volume up pressed");
}