因為app中用到華為推送,但是華為推送在不同版本上是存在不同問題的,需要單獨來處理。
那么最基本的問題是要獲取EMUI系統的版本號。
上網翻了很多博客帖子,基本上是在獲取root權限下去讀取/system/build.prop文件的內容,獲取Emui版本號。但是,不是所有用戶的手機都root了。
public static double getEMUI2() {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
} catch (IOException e) {
e.printStackTrace();
}
String property = properties.getProperty("ro.build.version.emui", "");
if (!TextUtils.isEmpty(property)) {
String substring = property.substring(property.length() - 3, property.length());
Double num = Double.valueOf(substring);
return num;
}
return 0;
}
此方法是直接去讀取系統文件獲取版本號。但是是在手機root的情況下才能獲取到。
用命令行打開此系統文件的方式是:
結果就可以看到該設備的所有信息
正片:
命令行獲取 adb shell getprop ,經本人用公司現有的測試手機測試,此命令行不管設備有沒有root,都可以獲取到設備信息
結果
通過代碼可得到
可以通過反射的方式獲取
public static String getEMUI() {
Class<?> classType = null;
String buildVersion = null;
try {
classType = Class.forName("android.os.SystemProperties");
Method getMethod = classType.getDeclaredMethod("get", new Class<?>[]{String.class});
buildVersion = (String) getMethod.invoke(classType, new Object[]{"ro.build.version.emui"});
} catch (Exception e) {
e.printStackTrace();
}
return buildVersion;
}