最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Utility to check browser agent - Stack Overflow

programmeradmin1浏览0评论

I have this simple utility code to check for browser User-Agent:

public class UserAgentHelper {
    public static boolean isMobile(){
        String userAgent = getUserAgent();
        return userAgent.contains("ipad");
    }
    public static boolean isAndroid(){
        return getUserAgent().contains("android");
    }
    public static native String getUserAgent() /*-{
        return navigator.userAgent.toLowerCase();
    }-*/;
}

I need to check if the browser is mobile or desktop. That is the main point. I'm starting with checking if the browser is iPad browser however this code fails sometimes when the string "ipad" is not there, when I tested on such browser sometimes the keyword is applewebkit, but who knows there could be more. So is there a

  • GWT library for this?
  • or a Javascript library for this?

I have this simple utility code to check for browser User-Agent:

public class UserAgentHelper {
    public static boolean isMobile(){
        String userAgent = getUserAgent();
        return userAgent.contains("ipad");
    }
    public static boolean isAndroid(){
        return getUserAgent().contains("android");
    }
    public static native String getUserAgent() /*-{
        return navigator.userAgent.toLowerCase();
    }-*/;
}

I need to check if the browser is mobile or desktop. That is the main point. I'm starting with checking if the browser is iPad browser however this code fails sometimes when the string "ipad" is not there, when I tested on such browser sometimes the keyword is applewebkit, but who knows there could be more. So is there a

  • GWT library for this?
  • or a Javascript library for this?
Share Improve this question asked May 2, 2014 at 9:46 quarksquarks 35.4k82 gold badges308 silver badges547 bronze badges 3
  • It might be better to check screen sizes than relying on a UserAgent that can be changed by anybody – Paul S. Commented May 2, 2014 at 9:50
  • Please have a look at What is the iPad user agent? – Braj Commented May 2, 2014 at 9:52
  • @PaulS.I saw this detectmobilebrowsers. – quarks Commented May 2, 2014 at 10:16
Add a ment  | 

3 Answers 3

Reset to default 2

There is a specialized library called UADetector which contains an extensive set of User-Agent entries.

Below is an example of using the UADetector inside a Servlet

import java.io.IOException;
import javax.servlet.*;
import net.sf.uadetector.service.UADetectorServiceFactory;
import net.sf.uadetector.UserAgent;
import net.sf.uadetector.UserAgentStringParser;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    PrintWriter out = response.getWriter();

    // Get an UserAgentStringParser and analyze the requesting client
    UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();
    ReadableUserAgent agent = parser.parse(request.getHeader("User-Agent"));

    out.append("You're a <em>");
    out.append(agent.getName());
    out.append("</em> of type <em>");
    out.append(agent.getType().getName());
    out.append("</em>!");
    }

}

The functionality you are looking for is in the ReadableUserAgent.getType() method. Also, take a look inside the OperatingSystemFamily class (and optionally UserAgentFamily) to see the exact list of agents you might be interested in.

NOTE: if you're using Maven for dependency management, here is the dependency you need to add to you pom.xml file.

NOTE: the UADetectorServiceFactory can also return an instance of UserAgentStringParser that updates daily with new User-Agents, otherwise it will just check the entries the library came with.

Try

    //Detect iPhone
    navigator.platform.indexOf("iPhone") != -1

    //Detect iPod
    navigator.platform.indexOf("iPod") != -1

For more info have a look at Detect iPad

Related to GWT we use in our application MGWT osDetection and Window's UserAgent (done by @ManuelCarrasco). Here is a snippet (notice that it could be useful knowing if Ipad has IOS7):

  public static String ua = GWT.isClient() ? Window.Navigator.getUserAgent().toLowerCase() : "JVM";

  public static boolean isUA_AndroidTablet() {
    return ua.contains("android") && !ua.contains("mobile");
  }

  public static boolean isMac = ua.matches(".*(ipad|macintosh).*");

  public static boolean isFF = ua.contains("firefox");

  public static boolean isIE = ua.contains("msie");

  public static boolean isIE8 = ua.contains("msie 8");

  public static boolean isIE9 = ua.contains("msie 9");

  public static boolean isChrome = ua.contains("chrome");

  //"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A465"
  //"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A501"
  public static boolean isIOS7 = ua.contains(" os 7_");

  public static final OsDetection os = GWT.isClient() ? MGWT.getOsDetection() : null;

  public static final boolean isPhone = GWT.isClient() && (os.isPhone() || os.isRetina());

  public static final boolean isIPad = GWT.isClient() && (os.isIPad() || os.isIPadRetina());

  public static final boolean isIpadOrIphone = isPhone || isIPad; 

Also if you do not want to include MGWT dependency, next code should do the trick:

public static String ua = GWT.isClient() ? Window.Navigator.getUserAgent().toLowerCase() : "JVM";
public static boolean isPad = ua.matches(".*(ipad).*");
发布评论

评论列表(0)

  1. 暂无评论