首先,我说我已经看到很多这样的问题,其中许多只是说< 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:加速度计无法正常运行全部内容,希望文章能够帮你解决Phonegap Build:加速度计无法正常运行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)