在 Android 中通过 Javascript 访问加速度计?

发布于 2024-10-08 01:05:37 字数 122 浏览 0 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

九厘米的零° 2024-10-15 01:05:37

从 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.

拥抱影子 2024-10-15 01:05:37

对此线程进行更新。

HTML5 可以让人们做到这一点。检测加速度计是否存在很容易。

    if (window.DeviceMotionEvent == undefined) {
        //No accelerometer is present. Use buttons. 
        alert("no accelerometer");
    }
    else {
        alert("accelerometer found");
        window.addEventListener("devicemotion", accelerometerUpdate, true);
    }

在您定义的用于接收加速度计事件的函数中,您可以查看 AccelerationInclusionGravity 成员。

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);
}

更多信息可以在这里找到: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.

    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.

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

梦里的微风 2024-10-15 01:05:37

您可以尝试使用 PhoneGap,它提供 API 从 javascript 访问加速度计。

此处文档。

You could try with PhoneGap that provides API to access the accelerometer from javascript.

Here the documentation.

述情 2024-10-15 01:05:37

如果您尝试从服务器上托管的网页访问加速计(而不是通过 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

扛刀软妹 2024-10-15 01:05:37

如果我正确阅读文档,您可以设置一个类(在 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.

无法言说的痛 2024-10-15 01:05:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文