1
0

More than 3 years have passed since last update.

EMUIバージョンの取得方法

Last updated at Posted at 2021-06-21

EMUIはファーウェイがAndroidをベースにカスタマイズしたものです。プログラムでは次の方法でEMUIのバージョンを取得できます。

public class EmuiInfo {

    public static EmuiInfo getEMUI() {
        EmuiInfo.Builder builder = new EmuiInfo.Builder();
        try {
            Class<?> classType = Class.forName("android.os.SystemProperties");
            Method getMethod = classType.getDeclaredMethod("get", new Class<?>[]{String.class});
            String buildVersion = (String) getMethod.invoke(classType, new Object[]{"ro.build.version.emui"});
            int startIndex = buildVersion.indexOf("_");
            if (startIndex != 1) {
                String emuiVersion = buildVersion.substring(startIndex + 1);
                String[] versionArray = emuiVersion.split("\\.");
                builder.major(versionArray[0]);
                builder.minor(versionArray[1]);
                builder.patch(versionArray[2]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return builder.build();
    }

    private String major = "";
    private String minor = "";
    private String patch = "";

    private EmuiInfo(Builder builder) {
        this.major = builder.major;
        this.minor = builder.minor;
        this.patch = builder.patch;
    }

    public String getMajor() {
        return this.major;
    }

    public String getMinor() {
        return this.minor;
    }

    public String getPatch() {
        return this.patch;
    }

    public String toString() {
        return this.major + "." + this.minor + "." + this.patch;
    }

    private static class Builder {
        private String major = "";
        private String minor = "";
        private String patch = "";

        public Builder() {
        }

        public Builder major(String major) {
            this.major = major;
            return this;
        }

        public Builder minor(String minor) {
            this.minor = minor;
            return this;
        }

        public Builder patch(String patch) {
            this.patch = patch;
            return this;
        }

        public EmuiInfo build() {
            return new EmuiInfo(this);
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0