在 Android 中通过 Javascript 访问加速度计?
有没有办法在 Android 浏览器上使用 Javascript 访问加速度计数据?我知道它支持“onorientationchange”,但我想得到一切。
澄清:我问的是如何在网站而不是本机应用程序中执行此操作。
Is there any way to access accelerometer data using Javascript on Android's browser? I know it supports "onorientationchange", but I'd like to get everything.
Clarification: I'm asking how to do this in a website, not a native app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 ICS、Android 4.0 开始,您可以通过 JavaScript 事件侦听器使用“devicemotion”事件来访问加速计数据。请参阅 W3C 文档了解如何访问它 - http://dev.w3.org/geo/ api/spec-source-orientation.html。
注意 - W3C 文档标题以“设备方向”命名,但规范确实包含“devicemotion”事件文档。
As of ICS, Android 4.0, you can use the 'devicemotion' event via a JavaScript event listener to access the accelerometer data. See the W3C documentation on how to access it - 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.
对此线程进行更新。
HTML5 可以让人们做到这一点。检测加速度计是否存在很容易。
在您定义的用于接收加速度计事件的函数中,您可以查看 AccelerationInclusionGravity 成员。
更多信息可以在这里找到:http://dev.w3.org/ geo/api/spec-source-orientation.html
Making an update to this thread.
HTML5 lets someone do this. Detecting whether or not an accelerometer is present is easy.
In the function that you define to receive the accelerometer events, you can look at the
accelerationIncludingGravity
member.More information can be found here: http://dev.w3.org/geo/api/spec-source-orientation.html
您可以尝试使用 PhoneGap,它提供 API 从 javascript 访问加速度计。
此处文档。
You could try with PhoneGap that provides API to access the accelerometer from javascript.
Here the documentation.
如果您尝试从服务器上托管的网页访问加速计(而不是通过
WebView
集成到本机应用程序中),那么目前加速计数据似乎不可用 适用于 Android。您可以在这里找到更详细的评估:http://www .mobilexweb.com/blog/android-froyo-html5-accelerometer-flash-player 。您可能还想查看此帖子:检测使用 JavaScript 在浏览器中旋转 Android 手机
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. You can find a more detailed assessment here: 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
如果我正确阅读文档,您可以设置一个类(在 Java/Android 中)来提供公共函数中所需的加速度计功能。
然后使用 addJavascriptInterface 调用为 webview 设置一个 javascript 接口,这使得该类中的公共函数可以从 javascript 中调用。
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.
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.
查看这篇文章flash.sensors.Accelerometer on Android inside web browser 看来加速度计数据可用于闪存。因此,一个可能的解决方法(至少对于具有闪存的设备)是一个小的闪存小程序,它可以为您抓取数据。
听起来像是黑客,但听起来仍然比用 Flash 制作整个东西要好
Looking at this post flash.sensors.Accelerometer on Android within web browser it seems accelerometer data is available to flash. 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