Android上HTML 5的设备方向问题

device orientation problem in HTML 5 on Android

本文关键字:方向 问题 HTML Android      更新时间:2023-09-26

我用下面的代码来检查html5中的设备方向和设备运动处理。我从http://dev.w3.org/geo/api/spec-source-orientation.html得到的。我用iPhone3G测试了我的代码,运动事件被捕获了。我不知道为什么设备方向没有被捕获。我相信由于iPhone3G没有罗盘支持,我无法捕获' compassneedcalibration '事件。

我在Android浏览器中测试了这款应用。(HTC野火)。我不能得到任何事件。请分享你的为什么我们没有得到事件在Android手机?

 window.addEventListener("compassneedscalibration", function(event) {
                  alert('Your compass needs calibrating! Wave your device in a figure-eight motion');
                  event.preventDefault();

     }, true);
    window.addEventListener("deviceorientation", function(event) {
                  // process event.alpha, event.beta and event.gamma
                  alert("deviceorientation");
    }, true);
    window.addEventListener("devicemotion", function(event) {
                  // Process event.acceleration, event.accelerationIncludingGravity,
                  // event.rotationRate and event.interval
                   alert("devicemotion");
    }, true);

更新1

我在iPhone和iPad上进行测试时注意到的一件事是,无论设备是否移动,事件都会连续触发,没有任何停止。

更新2

我找到了我在UPDATE 1中提出的问题背后的原因。该API每秒检测和传递加速度计数据50次。

我知道现在这些功能只在iOS Safari浏览器中实现。所以我们不能在iOS中捕获这些事件。

来源:http://www.mobilexweb.com/samples/ball.html

尝试在Android中监听"resize"事件。如果您正在使用jQuery,您的代码可能看起来像这样:

$(window).bind('resize', function() {
    alert('resize!');
});

在iOS上,你需要捕获的事件是"onorientationchange"