通过User-Agent获取操作系统信息和客户端浏览器判断
-
Java code
-
static
final
String[] MOBILE_SPECIFIC_SUBSTRING
=
{
"
iPad
"
,
"
iPhone
"
,
"
Android
"
,
"
MIDP
"
,
"
Opera Mobi
"
,
"
Opera Mini
"
,
"
BlackBerry
"
,
"
HP iPAQ
"
,
"
IEMobile
"
,
"
MSIEMobile
"
,
"
Windows Phone
"
,
"
HTC
"
,
"
LG
"
,
"
MOT
"
,
"
Nokia
"
,
"
Symbian
"
,
"
Fennec
"
,
"
Maemo
"
,
"
Tear
"
,
"
Midori
"
,
"
armv
"
,
"
Windows CE
"
,
"
WindowsCE
"
,
"
Smartphone
"
,
"
240x320
"
,
"
176x220
"
,
"
320x320
"
,
"
160x160
"
,
"
webOS
"
,
"
Palm
"
,
"
Sagem
"
,
"
Samsung
"
,
"
SGH
"
,
"
SIE
"
,
"
SonyEricsson
"
,
"
MMP
"
,
"
UCWEB
"
};
private
boolean
checkMobile() { String userAgent
=
request.getHeader(
"
user-agent
"
);
for
(String mobile: MOBILE_SPECIFIC_SUBSTRING){
if
(userAgent.contains(mobile)
||
userAgent.contains(mobile.toUpperCase())
||
userAgent.contains(mobile.toLowerCase())){
return
true
; } }
return
false
; }
但不能100%的判断准确。比如电脑用户可以在firefox浏览器设置User-Agent,可以模拟手机环境访问。
但我觉得以上代码我觉可以判断99%的手机用户了。
以下是绝大部分手机获取User-Agent的特点
1. “iPhone” – all iPhone models contain string “iPhone” in their user agent properties.
2. “Android” – same note as above with some exceptions.
3. “MIDP” – many mobile devices contain string “MIDP”. Some of this devices can have screen big enough to properly display regular version of your site. So think..
4. “Opera Mobi” – some of mobiles contain this string detecting mobile version of Opera.
5. “Opera Mini” – some of mobiles contain this string …
6. “BlackBerry” – one of most popular smartphones.
7. “HP iPAQ” – Hewlett Packard smartphone?
8. “IEMobile” – microsoft IE mobile version.
9. “MSIEMobile” – the same as above.
10.”Windows Phone” – microsoft mobile OS detection.
11.”HTC” – sometimes htc phones contain nothing specific but this.
12.”LG” – LG.
13.”MOT” – Motorola.
14.”Nokia” – just Nokia.
15.”Symbian” – Symbian OS detection.
16.”Fennec” – mobile Firefox.
17.”Maemo” – linux version from Nokia. Is it may be used only on phones?
18.”Tear” – some mobile browser from/for Nokia?
19.”Midori” – it seems like Nokia gays have nothing to do. For what so many browsers?
20.”armv6l” – some string detecting Nokia phones.
21.”armv7l” – .. above. “armv” can be used as generic version.
22.”Windows CE”.
23.”WindowsCE” – variant of above.
24.”Smartphone” – some smartphones (why not all?!) contain this string.
25.”240×320″ – .. above. I see the future: soon (already?) web designers will take these parameters directly to their site width and height.
26.”176×220″, “320×320″, “160×160″ – same as above.
27.”webOS” – Palm webOS detection.
28.”Palm” – just Palm.
29.”Sagem” – more less known phone.
30.”Samsung”.
31.”SGH” – Samsung.
32.”Siemens”, “SIE” – Siemens.
33.”SonyEricsson”.
34.”MMP” some phones contain this string in their user-agents.
35.”UCWEB” – just another mobile browser.
36. “iPad” – all iPad models contain string “iPad” in their user agent properties.