【问题标题】:PhoneGap alert system not workingPhoneGap 警报系统无法正常工作
【发布时间】:2015-08-01 23:34:30
【问题描述】:

所以我正在构建一个更大的应用程序,但我无法让这个简单的代码工作,这最终对我们的项目至关重要,我不知道为什么。我对 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 正常警报,也没有显示带有通知插件的警报。

【问题讨论】:

  • 我只是导入您的项目,警报工作正常。
  • 是的,它对我有用...

标签: javascript android html cordova phonegap-plugins


【解决方案1】:

您的代码中有两个错误:

  • 您的所有代码都必须在 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);
        }
    }
};

【讨论】:

  • 不,这也不行 :(。代码有意义,应该绝对可以工作,但 phonegap 今天没有它。模拟器只是告诉我设备准备好了。任何线索?(新代码在github)
  • 控制台显示的错误是什么?您使用的是phonegap 还是cordova,插件安装是否正确?
  • 嗨!感谢您的帮助,但我已经转移到不同的平台
猜你喜欢
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2017-10-27
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多