我有这个简单的实用程序代码来检查浏览器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();
}-*/;
}
我需要检查浏览器是移动还是桌面。这是重点。我开始检查浏览器是否是iPad浏览器,但是当字符串" ipad"不存在,当我在这样的浏览器上测试时,有时关键字是applewebkit,但谁知道可能会有更多。
是这样的答案 0 :(得分:2)
有一个名为UADetector的专用库,其中包含大量用户代理条目。
下面是在Servlet中使用UADetector的示例
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>!");
}
}
您正在寻找的功能在 ReadableUserAgent.getType() 方法。另外,请查看 OperatingSystemFamily 类(以及可选 UserAgentFamily ),查看您可能感兴趣的代理商的确切列表。
注意:如果您使用Maven进行依赖项管理,here是您需要添加到pom.xml文件的依赖项。
注意:UADetectorServiceFactory还可以返回UserAgentStringParser的实例,该实例每天都会使用新的User-Agents进行更新,否则它只会检查库附带的条目。
答案 1 :(得分:1)
尝试
//Detect iPhone
navigator.platform.indexOf("iPhone") != -1
//Detect iPod
navigator.platform.indexOf("iPod") != -1
有关详细信息,请查看Detect iPad
答案 2 :(得分:1)
与GWT相关,我们在我们的应用程序中使用MGWT osDetection和Window的UserAgent(由@ManuelCarrasco完成)。这是一个片段(请注意,知道Ipad是否有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;
此外,如果您不想包含MGWT依赖项,则下一个代码应该可以解决这个问题:
public static String ua = GWT.isClient() ? Window.Navigator.getUserAgent().toLowerCase() : "JVM";
public static boolean isPad = ua.matches(".*(ipad).*");