Android PhoneGap Accelerometer watchAcceleration数据不更新解决办法
最近再学phonegap,在测试accelerometer加速传感器(重力感应器)的watchAcceleration获取加速传感器数据时,发现第一次运行app时,watchAcceleration添加的success或者error回调都不执行,点击页面上的按钮调用震动功能后,watchAcceleration就正常执行,并且按照指定的frequency运行,但是传入success回调的数据不会更新。
经测试可能是phonegap的bug,出现这个错误phonegap的版本是2.9.0,我只是更换为cordova为2.9.1后再用eclipse编译项目,然后将apk安装到手机后就没有问题了。⊙﹏⊙b汗。
代码如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <meta name="viewport" content="minimum-scale=1, maximum-scale=1,width=device-width, height=device-height, target-densitydpi=device-dpi" /> <title>watchAcceleration test</title> </head> <body> <div id="acc"></div> <script type="text/javascript" src="cordova.js"></script><input type="button" onclick="executeNotification(this)" value="beep"/> <input type="button" onclick="executeNotification(this)" value="vibrate"/> <script type="text/javascript"> document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { navigator.accelerometer.watchAcceleration(onSuccess, onError, { frequency: 1000 }); document.getElementById('acc').innerHTML = 'waiting fro Acceleration info'; } function onError() { document.getElementById('acc').innerHTML = new Date().getTime() + '--get Acceleration info error'; } function onSuccess(d) { document.getElementById('acc').innerHTML =new Date().getTime()+ '--Acceleration info<br/>x:' + d.x + '<br>y:' + d.y + '<br>z:' + d.z + '<br>TS:' + d.timestamp; } function executeNotification(btn) { var v = btn.value; navigator.notification[btn.value](v == 'beep' ? 1 : v == 'vibrate' ? 500 : v); } </script> </body> </html>
在stackoverflow问的问题,没人回答,悲剧,嘿嘿:http://stackoverflow.com/questions/30770884/phonegap-accelerometer-does-not-update-data
加支付宝好友偷能量挖...
原创文章,转载请注明出处: Android PhoneGap Accelerometer watchAcceleration数据不更新解决办法