Phonegap Build:加速计无法运行android

时间:2014-08-13 09:14:59

标签: android cordova phonegap-build

首先我要说的是,我已经看到了很多这些问题,其中许多人只是说<script src="cordova-x-x-x.js"></script>而有些人说没有将cordova.js文件包含在Phonegap Build中。

所以我一直在测试和修复我的代码一段时间,但仍然从onError function收到错误。我还复制并粘贴了phonegap docs的代码。

所以这里是来自网址的干净代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    function onSuccess(acceleration) {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body>
  </body>
</html>

在我的config.xml文件中,我添加了:

<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.device-orientation" />

3 个答案:

答案 0 :(得分:3)

在测试并询问Phonegap Build工作人员后,他们向我展示了包含所有功能的代码。这是让加速度计与Phonegap Build一起使用的最佳方式

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

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

    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
        alert("Device is Ready");
        alert(device.available);
    }

    function getAcceleration(){
        navigator.accelerometer.getCurrentAcceleration(onAccelSuccess, onError);
    }

    function onAccelSuccess(acceleration) {
        var element = document.getElementById('accelerometer');
        element.innerHTML = 'Acceleration X: ' + acceleration.x         + '<br />' +
                            'Acceleration Y: ' + acceleration.y         + '<br />' +
                            'Acceleration Z: ' + acceleration.z         + '<br />' +
                            'Timestamp: '      + acceleration.timestamp + '<br />';
    }

    function onError() {
        alert('onError!');
    }

    function startWatch() {
        // Update acceleration every 1 seconds
        var options = { frequency: 1000 };

        watchID = navigator.accelerometer.watchAcceleration(onAccelSuccess, onError, options);
    }

    function stopWatch() {
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }
    </script>
  </head>
  <body onload="onLoad()">
    <p>
        <button onclick="getAcceleration()">Get Acceleration</button>
    </p>
    <p>
        <button onclick="startWatch()">Watch Acceleration</button>
    </p>
    <p>
        <button onclick="stopWatch()">Stop Watching Acceleration</button>
    </p>
    <div id="accelerometer">Waiting for accelerometer...</div>
  </body>
</html>

答案 1 :(得分:0)

我会说你尝试使用cordova完全清理重新安装/重建。首先,你必须为此安装cordova。如果您想知道,如何开始使用,您应该查看cordova文档:Cordova CLI-Docs

安装cordova后,进入终端并输入以下命令:

  • cd~ / desktop
  • cordova create test com.example.test test
  • cd test
  • cordova platform添加android
  • cordova插件添加org.apache.cordova.device-motion
  • cordova build

在Eclipse中打开构建的项目并添加

<!DOCTYPE html>
<html>
  <head>
    <title>Acceleration 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() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>getCurrentAcceleration</p>
  </body>
</html>

全部保存并将项目部署到您的设备。让我知道,如果这有效或无效。

答案 2 :(得分:0)

无法发表评论,所以请回答。

参考上述评论,“Phonegap和Cordova相似(相同的代码库)”

来自http://docs.build.phonegap.com/en_US/3.1.0/introduction_getting_started.md.html

  

为此,请确保在您的网站中进行以下引用   的index.html

     

&lt; script src =“phonegap.js”&gt;

     

请注意,这可以与

互换      

&lt; script src =“cordova.js”&gt;