Detecting Device Type in a web application

前端 未结 8 2191
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 08:48

We have a Java based application where in we want to detect the device type(mobile or desktop) for the device that is sending the request.

How is it possible?

相关标签:
8条回答
  • 2020-11-28 09:05

    You'll have to read the User-Agent header from the request and decide on that.

    In vanilla servlet apps, a crude way of doing it is:

    public void doGet(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {
      if(request.getHeader("User-Agent").contains("Mobi")) {
        //you're in mobile land
      } else {
        //nope, this is probably a desktop
      }
    }
    
    0 讨论(0)
  • 2020-11-28 09:09

    You can try this lib I think, yauaa can detect a user agent string is which device/software

    https://github.com/nielsbasjes/yauaa

    0 讨论(0)
  • 2020-11-28 09:10

    You can check the User-Agent HTTP header on the HttpServletRequest object.

    Ref: http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#user-agent

    0 讨论(0)
  • 2020-11-28 09:12

    You could get a 3rd party software solution. There are plenty of Open Source ones out there. I've used 51Degrees.mobi's Java solution before now (and have also worked on their open source C solution). Follow that link and hit the download button. It's relatively easy to get up and running.

    0 讨论(0)
  • 2020-11-28 09:16

    You can try to use Spring Mobile. There are convenient classes to solve that.

    Device currentDevice = DeviceUtils.getCurrentDevice(servletRequest);
    if(currentDevice.isMobile()) { /* Mobile */ }
    if(currentDevice.isTablet()) { /* Tablet */ }
    if(currentDevice.isNormal()) { /* Desktop */ }
    
    0 讨论(0)
  • 2020-11-28 09:17

    There's also http://openddr.mobi/, Open Source Device Detection for

    • Java
    • C#
    • VB.net

    based on the W3C Device Description Repository.

    OpenDDR is comparable to device repositories by 51Degrees, DeviceAtlas or ScientiaMobile/WURFL, but unlike those proprietary, closed source product, it's poen source and free to use.

    0 讨论(0)
提交回复
热议问题