[英]Access accelerometer via Javascript in Android?
Is there any way to access accelerometer data using Javascript on Android's browser? 有没有办法在Android的浏览器上使用Javascript访问加速度计数据? I know it supports "onorientationchange", but I'd like to get everything. 我知道它支持“onorientationchange”,但我想得到一切。
Clarification: I'm asking how to do this in a website, not a native app. 澄清:我问的是如何在网站上做到这一点,而不是原生应用。
As of ICS, Android 4.0, you can use the 'devicemotion' event via a JavaScript event listener to access the accelerometer data. 从ICS,Android 4.0开始,您可以通过JavaScript事件监听器使用“devicemotion”事件来访问加速度计数据。 See the W3C documentation on how to access it - http://dev.w3.org/geo/api/spec-source-orientation.html . 请参阅W3C文档,了解如何访问它 - http://dev.w3.org/geo/api/spec-source-orientation.html 。
Note - The W3C documentation title is named with 'device orientation', but the spec does indeed include 'devicemotion' event documentation. 注 - W3C文档标题以“设备方向”命名,但规范确实包含“devicemotion”事件文档。
Making an update to this thread. 对此线程进行更新。
HTML5 lets someone do this. HTML5让别人这样做。 Detecting whether or not an accelerometer is present is easy. 检测加速度计是否存在很容易。
if (window.DeviceMotionEvent == undefined) {
//No accelerometer is present. Use buttons.
alert("no accelerometer");
}
else {
alert("accelerometer found");
window.addEventListener("devicemotion", accelerometerUpdate, true);
}
In the function that you define to receive the accelerometer events, you can look at the accelerationIncludingGravity
member. 在您定义为接收加速度计事件的函数中,您可以查看accelerationIncludingGravity
成员。
function accelerometerUpdate(e) {
var aX = event.accelerationIncludingGravity.x*1;
var aY = event.accelerationIncludingGravity.y*1;
var aZ = event.accelerationIncludingGravity.z*1;
//The following two lines are just to calculate a
// tilt. Not really needed.
xPosition = Math.atan2(aY, aZ);
yPosition = Math.atan2(aX, aZ);
}
More information can be found here: http://dev.w3.org/geo/api/spec-source-orientation.html 更多信息可以在这里找到: http : //dev.w3.org/geo/api/spec-source-orientation.html
If you are trying to access the accelerometer from a webpage hosted on a server (verus one integrated into a native application through WebView
), than the accelerometer data does not appear to be available as of now for Android. 如果您尝试从服务器上托管的网页(通过WebView
集成到本机应用程序中的网页)访问加速度计,那么加速度计数据现在似乎不适用于Android。 You can find a more detailed assessment here: http://www.mobilexweb.com/blog/android-froyo-html5-accelerometer-flash-player . 您可以在此处找到更详细的评估: http : //www.mobilexweb.com/blog/android-froyo-html5-accelerometer-flash-player 。
You might also want to check out this SO post: Detect rotation of Android phone in the browser with JavaScript 您可能还想查看此SO帖子: 使用JavaScript在浏览器中检测Android手机的旋转
If I'm reading the docs correctly, you could set up a class (within Java/Android) that provides the accelerometer functionality you need in public functions. 如果我正确地阅读文档,您可以设置一个类(在Java / Android中),它提供公共函数中所需的加速度计功能。
Then setup a javascript interface for the webview using the addJavascriptInterface call, which makes the public functions in that class available to be called from within javascript. 然后使用addJavascriptInterface调用为webview设置一个javascript界面,这使得该类中的公共函数可以在javascript中调用。
Looking at this post flash.sensors.Accelerometer on Android within web browser it seems accelerometer data is available to flash. 在网络浏览器中看到这篇文章flash.sensors.Accelerometer在Android上 ,似乎加速计数据可用于闪存。 So a possible workaround (at least for devices which have flash) would be a small flash applet which grabbed the data for you. 因此,一个可能的解决方法(至少对于具有闪存的设备)将是一个小型闪存小程序,它为您抓取数据。
Sounds like a hack, but still sounds better than making the whole thing in flash 听起来像是一个黑客,但听起来仍然比用flash制作整个东西更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.