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

html - How to recognise tablet devices in browsers in java - Stack Overflow

programmeradmin0浏览0评论

Here is the code to recognize the tablet devices using the spring mobile dependency but it is not working and kept returning null, do you know any solution?


    public static boolean isTablet(HttpServletRequest request) {
        var res = ResolverUtils.isTablet(DeviceUtils.getCurrentDevice(request), getSitePreference(request));
        return res;
    }
    
    private static SitePreference getSitePreference(HttpServletRequest request) {
        String param = request.getParameter("site_preference");

        if ("normal".equalsIgnoreCase(param)) {
            return SitePreference.NORMAL;
        } else if ("mobile".equalsIgnoreCase(param)) {
            return SitePreference.MOBILE;
        } else if ("tablet".equalsIgnoreCase(param)) {
            return SitePreference.TABLET;
        }

        return null;
    }

Also I tried com.github.ua-parser but it is not recognizing the tablet devices too, here is my code for this one too :


    private static final Parser UA_PARSER = new Parser();

    
    public static boolean isTablet(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return false; // No User-Agent means we cannot detect the device
        }

        Client client = UA_PARSER.parse(userAgent);
        Device device = client.device;

        // Check if the device is a tablet (common pattern)
        return device.family != null && device.family.toLowerCase().contains("tablet");
    }

发布评论

评论列表(0)

  1. 暂无评论