栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JAVA】JAVA检测移动端IOS、Android工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JAVA】JAVA检测移动端IOS、Android工具类

目录

1. 移动端工具类

2. 移动端枚举类

参考资料


1. 移动端工具类

import com.easylive.mgs.gateway.enums.MobilePlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class UAgentInfo {

    // OS Detection

    private static final Pattern IOS_PATTERN = Pattern.compile("(.+) (.+) rv:([^(]+) \((.+); .+os (.+)");
    private static final Pattern ANDROID_PATTERN = Pattern.compile("; android (.+); (.+)\); (.+) android v(.+)", Pattern.CASE_INSENSITIVE);
    private static final Pattern ANDROID_WEB_PATTERN = Pattern.compile("; android (.+); (.+)\).+;web (.+) android v(.+)", Pattern.CASE_INSENSITIVE);
    private static final Logger logger = LoggerFactory.getLogger(UAgentInfo.class);
    // User-Agent and Accept HTTP request headers
    private String userAgent = "";
    private String appVersion = "";
    private String appName = "";
    private String osVersion = "";
    private String deviceModel = "";
    private String buildType = "";
    private String buildVersion = "";
    private MobilePlatform deviceType = MobilePlatform.UNKNOWN;

    
    public UAgentInfo(String userAgent) {
        init(userAgent);
    }

    
    public UAgentInfo(HttpServletRequest request) {
        String uagent = request.getHeader("User-Agent");
        String newua = null;
        try {
            newua = new String(uagent.getBytes("iso8859-1"), StandardCharsets.UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        init(newua);
    }

    private void init(String userAgent) {
        if (userAgent != null) {
            this.userAgent = userAgent.toLowerCase();
        }
        this.detection();

        if( this.appVersion.length() > 0 ) {
            String[] versions = this.appVersion.split("\.");
            if( versions.length > 3) {
                this.appVersion = String.format("%s.%s.%s", versions[0],versions[1],versions[2]);
            }
        }
    }

    private void detection() {
        Matcher androidMatcher = ANDROID_PATTERN.matcher(userAgent);
        if (androidMatcher.find()) {
            buildAndroidInfo(androidMatcher);
            return;
        }

        Matcher iosMatcher = IOS_PATTERN.matcher(userAgent);
        if (iosMatcher.find()) {
            this.appName = iosMatcher.group(1);
            this.appVersion = iosMatcher.group(2);
            this.osVersion = iosMatcher.group(5);
            this.deviceModel = iosMatcher.group(4);
            this.buildVersion = iosMatcher.group(3);
            String[] builds = this.buildVersion.split(" ");
            if (builds.length == 2) {
                this.buildType = builds[1];
                this.buildVersion = builds[0];
            }
            this.deviceType = MobilePlatform.IOS;
            return;
        }

        Matcher webAndroidMatcher = ANDROID_WEB_PATTERN.matcher(userAgent);
        if( webAndroidMatcher.find() ) {
            buildAndroidInfo(webAndroidMatcher);
        } else {
            this.appVersion = "0";
            this.deviceType = MobilePlatform.UNKNOWN;
        }
    }

    private void buildAndroidInfo(Matcher androidMatcher) {
        this.deviceType = MobilePlatform.ANDROID;

        this.appName = androidMatcher.group(3);
        this.appVersion = androidMatcher.group(4);
        this.osVersion = androidMatcher.group(1);
        this.deviceModel = androidMatcher.group(2);

        String[] builds = this.appVersion.split("-");
        if (builds.length >= 2) {
            if (builds[builds.length - 1].equals("mp")) {
                String[] _subbuilds = this.appVersion.split("_");
                if (_subbuilds.length == 2) {
                    String[] __subbuilds = _subbuilds[0].split(".");
                    this.buildType = _subbuilds[1].substring(0, _subbuilds[1].length() - 3);
                    if (__subbuilds.length >= 3) {
                        this.appVersion = __subbuilds[0] + "." + __subbuilds[1] + "." + __subbuilds[2];
                        if (__subbuilds.length >= 4) {
                            this.buildVersion = __subbuilds[3];
                        }
                    }

                }
            } else {
                this.buildType = builds[1];
                String[] __subbuilds = builds[0].split(".");
                if (__subbuilds.length >= 3) {
                    this.appVersion = __subbuilds[0] + "." + __subbuilds[1] + "." + __subbuilds[2];
                    if (__subbuilds.length >= 4) {
                        this.buildVersion = __subbuilds[3];
                    }
                }
            }
        }
    }

    public MobilePlatform getDeviceType() {
        return this.deviceType;
    }

    public String getAppName() {
        return this.appName;
    }
    public String getAppVersion() {
        return this.appVersion;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public String getOsVersion() {
        return osVersion;
    }

    public String getDeviceModel() {
        return deviceModel;
    }

    public String getBuildType() {
        return buildType;
    }

    public String getBuildVersion() {
        return buildVersion;
    }
}

2. 移动端枚举类
public enum MobilePlatform {

    UNKNOWN("unknown"),
    ANDROID("android"),
    IOS("ios"),
    PC("pc");

    private final String name;

    MobilePlatform(final String newValue) {
        name = newValue;
    }

    public String getName() {
        return name;
    }

    public static MobilePlatform fromName(String name) {
        if( name == null )
            return UNKNOWN;
        name = name.toLowerCase();

        if( IOS.getName().equals(name))
            return IOS;
        if( ANDROID.getName().equals(name))
            return ANDROID;
        if( PC.getName().equals(name))
            return PC;
        return UNKNOWN;
    }
}

参考资料

Detecting Mobile Devices Using Java :: Hand Interactive Resources

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号