【发布时间】:2014-01-27 15:01:25
【问题描述】:
我已经安装了 cordova(3.3 版)并使用这些命令构建了一个示例项目:
$ cordova create hello com.example.hello "HelloWorld"
$ cd hello
$ cordova platform add android
$ cordova build
并将项目导入 Eclipse(根据http://cordova.apache.org/docs/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide)。我可以通过选择 Run As → Android Application 从 Eclipse 成功运行该应用程序。
现在我想使用cordova 的通知功能。我使用以下命令添加了插件(遵循本指南:http://cordova.apache.org/docs/en/3.3.0/cordova_notification_notification.md.html#Notification):
$ cordova plugin add org.apache.cordova.dialogs
$ cordova plugin add org.apache.cordova.vibration
当我输入时:
$ cordova plugin ls
它正确地列出了我刚刚添加的插件。
我返回 Eclipse 并将以下代码粘贴到 assets/www/index.html 中(覆盖 index.html 中的现有代码):
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Empty
}
// Show a custom alert
//
function showAlert() {
navigator.notification.alert(
'You are the winner!', // message
'Game Over', // title
'Done' // buttonName
);
}
// Beep three times
//
function playBeep() {
navigator.notification.beep(3);
}
// Vibrate for 2 seconds
//
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
</body>
</html>
当我将它部署到设备时,它会显示三个链接(显示警报、播放哔声和振动)。当我按下这些时,我希望会发生相应的本机通知,但事实并非如此。相反,我收到以下错误消息(显示在 LogCat 中):
显示警报: 未捕获的 ReferenceError:未定义 showAlert:45
播放哔声:未捕获的引用错误:未定义播放哔声:46
振动:未捕获的参考错误:未定义振动:47
我应该如何修复这些错误?
提前致谢!
【问题讨论】:
标签: android eclipse plugins cordova-3