java-to determine whether the mobile or PC-side access

Source: Internet
Author: User

package com.hanchao.util;import java.util.regex.matcher;import java.util.regex.pattern;/**  *  @author  liweihan  ()  *  @version  1.0  (November 28, 2014   2:31:56)  *  Check whether the mobile Access  */public class CheckMobile { // \b  is the word boundary (two consecutive Alphabetic characters   and   non-alphabetic characters)   logical interval between,        //  strings are transcoded at compile time, So it is   "\\b"         // \B  is the logical interval between the words inside the word (a two-letter character that is attached)         static String phoneReg =  "\\b (IP (hone|od) | Android|opera m (ob|in) i "                 + "|windows  (phone|ce) |blackberry"                  + "|s (Ymbian|eries60|amsung) |p (LAYBOOK|ALM|ROFILE/MIDP"                  + "|laystation portable) |nokia|fennec|htc[-_]"                  + "|mobile|up.browser | [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";         static string tablereg =   "\\b (ipad|tablet| ( nexus 7) |up.browser "                 + "| [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";               // Mobile device regular match: mobile, tablet       static Pattern phonePat =  Pattern.compile (phonereg, pattern.case_insensitive);         Static pattern tablepat = pattern.compile (tablereg, pattern.case_insensitive);                 /**      *  detection is mobile device access       *        *  @Title: check      *  @Date  : 2014-7-7  Afternoon 01:29:07      *  @param  userAgent  Browser ID       *  @return  true: Mobile device access, FALSE:PC end Access        */      public static boolean check (String  useragent) {            if (null ==  useragent) {                 useragent =  "";            }             //  Matching               Matcher matcherphone = phonepat.matcher (useragent);             matcher matchertable = tablepat.matcher (userAgent);             if (Matcherphone.find ()  | |  matchertable.find ()) {                 return true;            }  else {                 return false;            }         }  }


Reference article: http://www.cnblogs.com/rubylouvre/archive/2012/04/11/2442588.html

http://blog.csdn.net/xiaoxian8023/article/details/37527133



This article is from the "My Java World" blog, so be sure to keep this source http://hanchaohan.blog.51cto.com/2996417/1584251

java-to determine whether the mobile or PC-side access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.