3

我是一个新的PhoneGap 程序员。navigator.notification.alert() 无法在 Android 上使用 phonegap。但它适用于 iPhone 和 WP7。我正在 Windows Phone 7 中开发这个应用程序。

谢谢你的帮助

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Cordova WP7</title>
    <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title"
        charset="utf-8" />
    <!--<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>-->  
    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript">
        function doAlert() {
            var message = "This is an Alert dialog";
            var title = "Attention!";
            navigator.notification.alert(message, title);
        };       

    </script>
</head>
<body>
    <h1>
        Cordova Tests</h1>
    <div id="info">
         <a href="#" onclick="doAlert()">Click Me</a>  
    </div>
</body>
</html>


最后,我得到了解决方案。

解决方案:

在我们构建 phonegap 代码之前(在build.phonegap.com中),我们需要删除cordova-2.0.0.js文件。如果我们删除这个cordova-2.0.0.js,phonegap 代码正在安卓上运行。

4

1 回答 1

2

你必须等待DeviceReady

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale = 1.0, maximum-scale=1.0, user-scalable=no" />
<script type="text/javascript" charset="utf-8" src="windows/cordova-2.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
    var deviceReady = false;
    function init() {
        document.addEventListener("deviceready", function () {
            deviceReady = true;
        }, false);

        window.setTimeout(function () {
            if (!deviceReady) {
                alert("Error: Phonegap did not initialize.  Demo will not run correctly.");
                console.log("Error: Phonegap did not initialize.  Demo will not run correctly.");
            }
        }, 1000);
    }

    function doAlert() {
        var message = "This is an Alert dialog";
        var title = "Attention!";
        navigator.notification.alert(message, title);
    }
</script>
</head>
<body onLoad="init();">
<h1>Cordova Tests</h1>
<div id="info">
    <button onclick="doAlert();">Click Me</button>
</div>
</body>
</html>
于 2012-09-12T19:53:07.680 回答