android - PhoneGap - OnDeviceReady 方法未被调用

标签 android cordova

我正在开发我的第一个应用程序phonegap(android)。

index.html

 <!DOCTYPE html>
    <html>
      <head>
        <title>Device Properties Example</title>

        <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
        <script type="text/javascript" charset="utf-8">
        alert('code: 1');
        // Wait for Cordova to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);
        alert('code: 2');
        var watchID = null;
        alert('code: 3');
        // Cordova is ready
        //
        function onDeviceReady() {
            // Update every 3 seconds
            alert('code: 4');
            var options = { frequency: 3000 };
            watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
        }

        // onSuccess Geolocation
        //
        function onSuccess(position) {
            alert('code: 5');
            var element = document.getElementById('geolocation');
            element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                                'Longitude: ' + position.coords.longitude     + '<br />' +
                                '<hr />'      + element.innerHTML;
        }

        // clear the watch that was started earlier
        // 
        function clearWatch() {
            alert('code: 6');
            if (watchID != null) {
                navigator.geolocation.clearWatch(watchID);
                watchID = null;
            }
        }

        // onError Callback receives a PositionError object
        //
        function onError(error) {
          alert('code: '    + error.code    + '\n' +
                'message: ' + error.message + '\n');
        }

        </script>
      </head>
      <body>
        <p id="geolocation">Watching geolocation...</p>
        <button onclick="clearWatch();">Clear Watch</button>     
      </body>
    </html>

这里 onDeviceReady 方法没有被调用。请帮助我了解我所缺少的内容。

我已添加这些权限

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

在manifest.xml 文件中。

最佳答案

按照这个方法操作,应该可以正常工作。

<!DOCTYPE html>
<html>
  <head>
    <title>Cordova Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Call onDeviceReady when Cordova is loaded.
    //
    // At this point, the document has loaded but cordova-1.7.0.js has not.
    // When Cordova is loaded and talking with the native device,
    // it will call the event `deviceready`.
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // Cordova is loaded and it is now safe to make calls Cordova methods
    //
    function onDeviceReady() {
        // Now safe to use the Cordova API
    }

    </script>
  </head>
  <body onload="onLoad()">
  </body>
</html>

另请检查Android example folder在 Cordova 1.7 下载中。

关于android - PhoneGap - OnDeviceReady 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893322/

相关文章:

jquery - Phonegap 2.2 Windows 8 :app package for Win Store using Win Certificate Kit - failed with error

ios - XCode 中的 Clang 错误

android - 如何使support.v4.widget.SlidingPaneLayout Pane 向左滑动

android - 聚焦时,如何使 EditText 与键盘一起升高?

安卓奥利奥 : what should I do to publish my app as an Autofill service provider?

android - 使用 HttpUrlConnection 向服务器发送请求

android - 使用线性布局在真实设备上未显示图像

javascript - iOS:使用 XMLHttpRequest 进行身份验证 - 处理 401 响应

javascript - ajax 完成后清除 Phonegap 应用程序上的缓存或清除 javascript 变量

android - 使用 Cordova 的 Azure 通知中心