管灌系统巡查员智能手机App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cc.shinichi.library.tool.common
 
import android.text.TextUtils
 
object DeviceUtil {
 
    /**
     * 是否为鸿蒙系统
     *
     * @return true为鸿蒙系统
     */
    fun isHarmonyOs(): Boolean {
        return try {
            val buildExClass = Class.forName("com.huawei.system.BuildEx")
            val osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass)
            osBrand.toString().contains("harmony", ignoreCase = true)
        } catch (x: Throwable) {
            false
        }
    }
 
    /**
     * 获取鸿蒙系统版本号
     *
     * @return 版本号
     */
    fun getHarmonyVersion(): String? {
        return getProp("hw_sc.build.os.version", "")
    }
 
    /**
     * 获取鸿蒙系统版本号
     * 鸿蒙2.0版本号为6
     * 鸿蒙3.0版本号为8
     * @return 版本号
     */
    fun getHarmonyVersionCode(): Int {
        return getProp("hw_sc.build.os.apiversion", "0")?.toInt() ?: 0
    }
 
    private fun getProp(property: String, defaultValue: String): String? {
        try {
            val spClz = Class.forName("android.os.SystemProperties")
            val method = spClz.getDeclaredMethod("get", String::class.java)
            val value = method.invoke(spClz, property) as String
            return if (TextUtils.isEmpty(value)) {
                defaultValue
            } else value
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return defaultValue
    }
}