Phonegap Build:加速度计在Android上无法工作

3
首先我想说,我看到了很多类似的问题,其中许多人只是说需要在<script src="cordova-x-x-x.js"></script>中添加cordova.js文件,而有些则不需要在Phonegap Build中包含该文件。
因此,我已经测试和修复了我的代码一段时间,但仍然从onError function中得到错误。我还从phonegap文档中复制并粘贴了代码。
所以这里是来自URL的干净代码:
<!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个回答

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>

即使使用这段代码(我非常确定是正确的),我仍然遇到问题。LogCat 显示在调用 watchAcceleration(...) 后,我正在使用 BOSCH BMA250 3 轴加速度计(HTC One M7)。据我所知,没有错误... 但我没有收到任何回调(无 onSucces... noError)。 - Dois

0

0

我建议您尝试使用cordova进行完全的清洁重新安装/重建。首先,您需要安装cordova。如果您想知道如何入门,请查看cordova文档:Cordova CLI-Docs

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

  • cd ~/desktop
  • cordova create test com.example.test test
  • cd test
  • cordova platform add android
  • cordova plugin add 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>

保存所有更改并将项目部署到您的设备。请告诉我,这是否有效。


我正在使用Phonegap Build,因此无需重新安装cordova / phonegap。 - Marius Djerv
如果你不想尝试任何东西,因为你认为你知道那个解决方案,那么请随意尝试任何东西,但不要在这里寻求帮助。Phonegap和Cordova很相似(同一代码库)。但是有一些差异(谷歌一下)。即使这些差异很小,也有可能使用cordova使其正常工作。http://stackoverflow.com/questions/24605033/new-phonegap-project-always-list-helloworld/24605227#24605227 - Sithys
那么,我需要在我的电脑上安装cordova/phonegap,下载Eclipse,安装所有必要的软件并设置工作区等。我正在使用基于云的Phonegap Build,因此没有重新安装任何东西的选项。但是我可以稍后尝试这个方法,需要下载所有必要的软件和Android SDK。 - Marius Djerv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接