Detecting Device Type in a web application

…衆ロ難τιáo~ 提交于 2019-11-26 22:15:39

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").indexOf("Mobile") != -1) {
    //you're in mobile land
  } else {
    //nope, this is probably a desktop
  }
}
nidhin

You can get device information by parsing http header

String browserType = request.getHeader("User-Agent");

You should parse browserType to get device type

This may help

  public String  getBrowserInfo( String Information )
  {
    String browsername = "";
    String browserversion = "";
    String browser = Information;
    if (browser.contains("MSIE"))
    {
      String subsString = browser.substring(browser.indexOf("MSIE"));
      String info[] = (subsString.split(";")[0]).split(" ");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Firefox"))
    {

      String subsString = browser.substring(browser.indexOf("Firefox"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Chrome"))
    {

      String subsString = browser.substring(browser.indexOf("Chrome"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Opera"))
    {

      String subsString = browser.substring(browser.indexOf("Opera"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Safari"))
    {

      String subsString = browser.substring(browser.indexOf("Safari"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    }
    return browsername + "-" + browserversion;
  }

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.

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

https://github.com/nielsbasjes/yauaa

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 */ }

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

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

For Java 51Degrees has a free API on GitHub https://github.com/51Degrees/Java-Device-Detection, it is undated regularly and should be able to cover all the basic detentions that you need.

There is a tutorial how to use the API for Device Type detection here https://51degrees.com/Developers/Documentation/APIs/Java-V32/Tutorials/Web-App

There is also a free cloud version that offers the DeviceType Property (if you want more than just IsMobile) you can find the details on this on the 51Degrees website. You will need to "purchase" the free cloud through the website, but it does not ask for bank details and is completely free. You can also use JavaScript and other integration dependant on your requirements.

Disclosure: I work at 51Degrees.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!