gpt4 book ai didi

android - Phonegap 版本 : accelerometer not working android

转载 作者:太空狗 更新时间:2023-10-29 12:41:16 25 4
gpt4 key购买 nike

首先我要说的是,我看到了很多这样的问题,其中很多只是说出来<script src="cordova-x-x-x.js"></script>有人说不要将 cordova.js 文件包含到 Phonegap Build 中。

所以我已经测试和修复我的代码一段时间了,但仍然收到来自 onError function 的错误。 .我还复制并粘贴了 phonegap docs 中的代码.

所以这是来自 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" />

最佳答案

在测试并询问 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>

关于android - Phonegap 版本 : accelerometer not working android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25282405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com