Phonegap Build:加速度计无法正常运行
作者:互联网
首先,我说我已经看到很多这样的问题,其中许多只是说< script src =“ cordova-x-x-x.js”< / script>有人说不将cordova.js文件包含到Phonegap Build中.
因此,我已经测试和修复了一段时间的代码,仍然从onError函数得到错误信息.我还复制并粘贴了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, one rror);
}
function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}
function one rror() {
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 onl oad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
alert("Device is Ready");
alert(device.available);
}
function getAcceleration(){
navigator.accelerometer.getCurrentAcceleration(onAccelSuccess, one rror);
}
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 one rror() {
alert('onError!');
}
function startWatch() {
// Update acceleration every 1 seconds
var options = { frequency: 1000 };
watchID = navigator.accelerometer.watchAcceleration(onAccelSuccess, one rror, options);
}
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
</script>
</head>
<body onl oad="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>
标签:phonegap-build,cordova,android 来源: https://codeday.me/bug/20191121/2051349.html