PhoneGap警报系统无法正常工作

时间:2015-08-01 23:34:30

标签: javascript android html cordova phonegap-plugins

所以我正在构建一个更大的应用程序,但是我无法使这个简单的代码工作,这将最终成为我们的项目必不可少的,我无法找出原因。我是Phonegap的新手,所以任何帮助都很棒。这是我的代码: https://github.com/dezert99/RIME/blob/master/www/index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="index.js"></script>
        <script type="text/javascript">
            app.initialize();
    </body>
</html>

index.js文件:https://github.com/dezert99/RIME/blob/master/www/index.js 正常警报以及带有通知插件的警报都会显示出来。

1 个答案:

答案 0 :(得分:0)

您的代码中有两个错误:

  • 所有代码都必须在触发deviceready事件后运行。

  • 不要混合使用内联代码和外部代码。

这意味着:

删除HTML文档中的javascript代码,但以下行除外:

<script type="text/javascript">
            app.initialize();
</script>

app.initialize()正在启动你的应用程序逻辑,它位于index.js

修改你的index.js(代码未测试!):

var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function () {
        alert("purple lizards");
        navigator.notification.alert("Hi", alertDismissed, 'test', 'yay');
        function alertDismissed(){
            // Do something when alert has error
        }
        app.showGeoLocation();
    }, showGeolocation: function () {

        navigator.geolocation.getCurrentPostion(onSuccess, onError);

        function onSuccess(position) {
            navigator.notification.alert(
                'Lat: ' + position.coords.latitude + '\n' + 'Lon: ' + position.coords.longitude,  // message
                alertDismissed,         // callback
                'Location',            // title
                'YAY'// buttonName
            )
        }

        function onError(error) {
            alert('code: ' + error.code + '\n' + 'message: ' + error.message);
        }
    }
};