简体   繁体   中英

How to install and call cordova plugins

I have installed cordova (version 3.3) and built a sample project using these commands:

$ cordova create hello com.example.hello "HelloWorld"
$ cd hello
$ cordova platform add android
$ cordova build

and imported the project into Eclipse (according to http://cordova.apache.org/docs/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide ). I am able to successfully run the app from Eclipse by selecting Run As → Android Application .

Now I want to make use of cordova's notification ablities. I added the plugins (following this guide: http://cordova.apache.org/docs/en/3.3.0/cordova_notification_notification.md.html#Notification ) with the commands:

$ cordova plugin add org.apache.cordova.dialogs
$ cordova plugin add org.apache.cordova.vibration

and when I type:

$ cordova plugin ls

it correctly lists the plugins I just added.

I return to Eclipse and paste the following code into assets/www/index.html (overwriting the existing code in 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>

When I deploy it to a device, it displays three links (Show Alert, Play Beep and Vibrate). When I press on these I expect the corresponding native notification to occur, but it doesn't. Instead I get the following error messages (displayed in LogCat):

Show alert: Uncaught ReferenceError: showAlert is not defined:45

Play Beep: Uncaught ReferenceError: playBeep is not defined:46

Vibrate: Uncaught ReferenceError: vibrate is not defined:47

How am I supposed to fix these errors?

Thanks in advance!

In your question you didn't mention updating the config.xml and AndroidManifest.xml files as noted in the API documentation. I'll copy them here for reference.

(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" />

For further clarification see the answer to this question. Should a phonegap plugin be declared in the config.xml file?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM