【发布时间】:2016-03-22 10:55:54
【问题描述】:
我正在为 Android 4.4 设备编写一个 cordova(版本 6)应用程序。在这里,我试图捕捉按钮按下事件,例如降低音量 按钮。不幸的是,我需要在服务器上托管应用程序。这意味着所有的 html、css 和 js 文件都是远程加载的。而应用程序本身的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");
}
谁能告诉我,为什么这段代码不再工作并帮我解决这个问题?
【问题讨论】:
标签: javascript java android cordova event-handling