2014-01-27 101 views
0

我已经安装了科尔多瓦(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成功运行应用程序。

现在我想利用科尔多瓦的通知能力。我加入了插件(这个指南: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和下面的代码粘贴到资产/ 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> 

当我将其部署到一个设备,它显示三个链接(Show Alert,Play Beep和Vibrate)。当我按这些时,我期望相应的本地通知发生,但它不会。相反,我得到了以下错误消息(显示在logcat中):

显示警报:未捕获的ReferenceError:showAlert没有定义:45

播放提示音:未捕获的ReferenceError:未定义playBeep:46

振动:未捕获的ReferenceError:振动没有定义:47

我应该如何修复这些错误?

在此先感谢!

回答

0

您是否尝试更新文件并运行www文件夹中的所有内容?

+0

是的,这是正确的!非常感谢您的注意:D – LittleGoldFish

0

在你的问题中,你没有提到更新API文档中提到的config.xml和AndroidManifest.xml文件。我将把它们复制到这里以供参考。

(in app/res/xml/config.xml) 
<feature name="Notification"> 
    <param name="android-package" value="org.apache.cordova.dialogs.Notification" /> 
</feature> 
<feature name="Vibration"> 
    <param name="android-package" value="org.apache.cordova.vibration.Vibration" /> 
</feature> 


(in app/AndroidManifest.xml) 
<uses-permission android:name="android.permission.VIBRATE" /> 

有关进一步的说明,请参阅此问题的答案。 Should a phonegap plugin be declared in the config.xml file?

+0

感谢您的回复! 我没有提到更新文件,因为我注意到添加插件时app/res/xml/config.xml和app/AndroidManifest.xml已经自动更新。 – LittleGoldFish