简体   繁体   English

Android手机浏览器检测

[英]Android Phone Browser Detection

I have an web application where mobile phone users see a mobile optimized website. 我有一个Web应用程序,手机用户可以看到移动优化的网站。 The new Samsung Galaxy SIII user agent provides no clue that the request is coming from a mobile phone. 新的三星Galaxy SIII用户代理无法提供请求来自手机的线索。 What are the best pratices to detect mobile phones? 检测手机有哪些最佳实践?

I'm aware of javascript feature detection (ie. modernizer) but am hoping for something better. 我知道javascript特征检测(即现代化),但我希望有更好的东西。 Below is the Samsung Galaxy SIII user agent: 以下是三星Galaxy SIII用户代理:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.34 Safari/534.24 Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 534.24(KHTML,与Gecko一样)Chrome / 11.0.696.34 Safari / 534.24

EDIT: The SIII has two possible user agents. 编辑:SIII有两个可能的用户代理。 Above is the 'desktop' version. 以上是“桌面”版本。 Fun stuff. 好玩的东西。 See link below for details. 请参阅以下链接了解详情。

http://www.anandtech.com/Show/Index/5310?cPage=19&all=False&sort=0&page=5&slug=samsung-galaxy-nexus-ice-cream-sandwich-review http://www.anandtech.com/Show/Index/5310?cPage=19&all=False&sort=0&page=5&slug=samsung-galaxy-nexus-ice-cream-sandwich-review

Looking at that user agent, I'd have to say that, that would be extremely difficult to differentiate from a non-handheld device. 看看那个用户代理,我不得不说,与非手​​持设备区别开来是非常困难的。

The problem with browser detection is that it's obviously easy to tweak the user agent string, and thus you never really know if what the server is telling you is honest or not. 浏览器检测的问题在于,显然很容易调整用户代理字符串,因此您从未真正知道服务器告诉您的是否诚实。

You have two options in this case: 在这种情况下,您有两种选择:

  • You can check every single header the phone sends and maybe see if there is one that could make it unique 您可以检查手机发送的每个标头,也许可以查看是否有一个可以使其唯一的标头

  • Or find some type of work-around by testing page load time etc..., As a whimsical example, browsers on handheld devices usually render pages a bit slower than their desktop counterparts, so after testing every possible mobile device with something like : 或者通过测试页面加载时间等找到某种类型的解决方法......,作为一个异想天开的例子,手持设备上的浏览器通常会使页面比桌面版本慢一些,所以在测试每个可能的移动设备后, 例如

- -

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
 // some code..
}

You can see if a page with nothing else but a simple script is not loading in it's ideal time. 您可以查看除了简单脚本之外没有其他任何内容的页面是否未加载到它的理想时间。

You get the point. 你明白了。

Also, try going here and see if it can detect you: http://detectmobilebrowsers.com/ 此外,尝试去这里,看看它是否能检测到你: http//detectmobilebrowsers.com/

The "desktop mode" user agent is designed so that the web server doesn't deliver a mobile/tablet interface when the user has deliberately chosen to have the desktop mode - it is a usability anti-pattern to remove that choice from the user. “桌面模式”用户代理的设计使得当用户故意选择使用桌面模式时,Web服务器不提供移动/平板电脑界面 - 从用户中删除该选择是一种可用性反模式。

If you absolutely *must* override the user's choice, then in JavaScript currently (2013) if all the following are true then it very likely is an Android device: 如果您绝对 *必须*覆盖用户的选择,那么在JavaScript当前(2013)如果满足以下所有条件,则很可能是Android设备:

  1. 'ontouchstart' in document.documentElement
  2. navigator.vendor === 'Google Inc.'
  3. /^Linux armv/.test(navigator.platform)

However the above will give false positives and negatives with some less common browser setups eg an ARM based ChromeBook with touch, eg maybe Chromium running on Linux with an ARM processor (although perhaps that is Linux/ARM), eg maybe other browsers Firefox/Opera etc eg an Android device that is not touch based like a TV eg can Chrome run on Windows RT with an ARM processor? 然而,上面的一些不太常见的浏览器设置会给出误报和否定,例如基于ARM的触摸ChromeBook,例如可能在Linux上使用ARM处理器运行Chromium(尽管可能是Linux / ARM),例如可能是其他浏览器Firefox / Opera例如,像电视一样没有触摸的Android设备,例如Chrome可以在带有ARM处理器的Windows RT上运行吗? eg an Android running on Intel. 例如在英特尔上运行的Android。

I have to detect "desktop view" after all, so similar to robocat's answer this is what I'm using to detect 'desktop view'. 毕竟我必须检测“桌面视图”,所以类似于robocat的答案,这就是我用来检测“桌面视图”的内容。 It's not perfect but it seems to provide the right balance of avoiding false positives: 它并不完美,但它似乎提供了避免误报的正确平衡:

 if (userAgent.indexOf('X11; Linux x86_64') !== -1 && 'ontouchstart' in document.documentElement && /^Linux armv/.test(navigator.platform) && _constructor.isChrome) { if ((window.outerWidth < 500 && window.outerHeight < 800) || (window.outerWidth < 800 && window.outerHeight < 500)) { // do stuff. } } 

Helped me decide what the width / height filter should be 帮我决定宽度/高度过滤器应该是什么

http://mydevice.io/devices/ http://viewportsizes.com/?filter= http://mydevice.io/devices/ http://viewportsizes.com/?filter=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM