使用Android设备上的JavaScript检测方向 [英] Detect orientation using javascript on android devices

查看:134
本文介绍了使用Android设备上的JavaScript检测方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一些code <一个href=\"http://stackoverflow.com/questions/1649086/detect-rotation-of-android-phone-in-the-browser-with-javascript\">this问题我已经建立了一些code当一个Android设备旋转检测。它的伟大工程的华硕平板(4.0.3)和两个模拟器(4.0.3和2.1),但Kindle Fire的(2.3.4),和droidx(2.3.4)它切换的宽度和高度。

Using some code from this question I have set up some code to detect when an android device is rotated. It works great for the asus tablet (4.0.3) and two simulators (4.0.3 and 2.1), but for the kindle fire (2.3.4), and droidx (2.3.4) it switches the width and height.

code:

<script type="text/javascript">
    var supportsOrientationChange = "onorientationchange" in window,orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

    window.addEventListener(orientationEvent, function() {
        alert("The rotation is " + window.orientation + " and the resolution is " + screen.width + " x " + screen.height);
        modRule();
    }, false);
</script>

这是华硕平板输出

把它保持在什么样子的风景:

Holding it in what looks like landscape:

旋转为0,分辨率为1280 x 800

The rotation is 0 and the resolution is 1280 x 800

纵向

旋转是-90,分辨率为800×1280

The rotation is -90 and the resolution is 800 x 1280

这是Kindle Fire的输出

Output from Kindle Fire

风景

旋转为90,分辨率为600 x 819

The rotation is 90 and the resolution is 600 x 819

人像:

的旋转为0,分辨率为1024×395

The rotation is 0 and the resolution is 1024 x 395

从droidx输出

景观:

的旋转是90和分辨率为320x488

The rotation is 90 and the resolution is 320x488

人像:

的旋转为0,分辨率为569x239

The rotation is 0 and the resolution is 569x239

有没有一种方法,我可以

Is there a way I can

a)使JavaScript的检测是否应该使用,而不是宽度高度或宽度,而不是高度

a) Make the javascript detect if it should use height instead of width or width instead of height

二)使得该设备为他们的宽度和高度报告正确的价值观?

b) Make the devices report the correct values for their width and height?

推荐答案

找了一段时间后,我发现,这与2.2和2.3操作系统的错误。我将这个标签code在我的应用程序固定2.3.4的bug。

After looking for a while longer I found out that this is a bug with the 2.2 and 2.3 OS. I fixed the bug with 2.3.4 by putting this code in my app.

browser = (WebView)findViewById(R.id.webBrowser);
browser.setBackgroundColor(Color.BLACK);
WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString("Android " + android.os.Build.VERSION.SDK);//this is so the JavaScript knows what version of the OS I'm using

和再检测,如果我在横向模式下:

And then for detecting if I'm in landscape mode:

var uagent = navigator.userAgent.toLowerCase();
function isLandscape()
{
    var width = screen.width;
    var height = screen.height;
    if (isBugged())
    {
        var temp = width;
        width = height;
        height = temp;
    }
    var landscape = width > height;
    return landscape;
}

function isBugged()
{
    return uagent == "android 10"
}

如果这还不够混乱,当身体最初加载,这是对的,如果它在横向模式或没有。因此,我不得不绕过我自己的解决方法。

And if that wasn't confusing enough, when the body initially loads, it's right about if it's in landscape mode or not. So I had to bypass my own workaround.

<body onload="if(isBugged()){uagent = 'bypass';}/*code that depends on isLandscape()*/;uagent = navigator.userAgent.toLowerCase();">

这是一个真正的痛苦,很多工作比它应该是。特别是因为它工作在2.1而不是2.3.4。很无奈,但这就是我。此刻,我只检查SDK 10,我会很快增加检查其他窃听的版本。

It's a real pain, a lot more work than it should be. Especially since it works in 2.1 but not 2.3.4. Really frustrating, but that's what I have. At the moment, I only check for sdk 10, I'm going to add checking for the other bugged versions soon.

这篇关于使用Android设备上的JavaScript检测方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆