原文: Android 獲取設備的CPU型號和設備型號-Stars-One的雜貨小窩 之前整的項目的總結信息,可能不太全,湊合著用吧,代碼在最下麵一節 CPU型號數據 華為: ro.mediatek.platform vivo: ro.vivo.product.platform oppo: ro.b ...
之前整的項目的總結信息,可能不太全,湊合著用吧,代碼在最下麵一節
CPU型號數據
- 華為: ro.mediatek.platform
- vivo: ro.vivo.product.platform
- oppo: ro.board.platform 或 ro.product.board
- 三星: ro.board.platform
- 小米: ro.soc.model
小米:
GKI 2.0 之前,/proc/cpuinfo 中包含 Hardware 相關信息,應用可以從中讀取 cpu 型號信息;
GKI 2.0 之後,/proc/cpuinfo 中不再包含 Hardware 相關信息,應用無法從中讀取到 cpu 型號信息,需要從 android.os.SystemProperties 中讀取 ro.soc.model 屬性信息。參考說明:關於 GKI 內核版本說明
建議優先從 android.os.SystemProperties 中讀取 ro.soc.model 屬性信息,若此屬性信息為空,則再從 /proc/cpuinfo 中讀取 Hardware 信息即可。
1.查看所有配置信息:
adb shell getprop
2.查看指定配置信息:
adb shell getprop [key]
如:查看設備型號:adb shell getprop “ro.product.model”
可以通過adb shell命令來得到數據或者通過反射
//通過shell命令
val cmdResult = ShellUtils.execCmd("getprop", false)
//通過反射
val classType = Class.forName("android.os.SystemProperties")
val method = classType.getDeclaredMethod("get", String::class.java)
val result = method.invoke(classType, "ro.vivo.product.platform") as String //SM6225
設備型號
vivo
[ro.vivo.os.build.display.id]: [OriginOS 1.0]
[ro.vivo.os.name]: [Funtouch]
[ro.vivo.os.version]: [12.0]
[ro.vivo.market.name]: [vivo Y32]
小米
新增ro.product.marketname屬性,值為手機設備名(上市機型名稱);
歷史已發佈手機上設備型號的屬性ro.product.model依然有效,以後新發佈的機型上不再生效。請開發者及時適配。
建議應用優先讀取屬性ro.product.marketname,如果返回值不為空,這個值就是手機設備型號。如果此屬性返回值為空,則讀取之前的屬性ro.product.model值
public static String getDeviceName() {
String deviceName = "";
try {
Class SystemProperties = Class.forName("android.os.SystemProperties");
Method get = SystemProperties.getDeclaredMethod("get", String.class, String.class);
deviceName = (String) get.invoke(SystemProperties, "ro.product.marketname", "");
if (TextUtils.isEmpty(deviceName)) {
deviceName = (String) get.invoke(SystemProperties, "ro.product.model", "");
}
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return deviceName;
}
工具類源碼
點擊展開代碼
package com.hlfonts.richway.widget.tool
import android.os.Build
import android.text.TextUtils
import com.blankj.utilcode.util.ShellUtils
import java.util.*
/**
* 工具類參考 https://blog.csdn.net/convex1009848621/article/details/120767000
*/
object CustomOSUtils {
/**
* customOS預設值為"",如果識別出的手機廠商是預知的,會被重新賦值,如果未識別到該機型則返回原生安卓信息
*/
private var customOS: String? = ""
/**
* CustomOSVersion預設值為"",如果識別出的手機廠商是預知的,會被重新賦值成對應rom系統的版本號
* 如果未識別到該機型則返回原生安卓信息
*/
private var customOSVersion: String? = ""
/**
* HarmonyOS 系統輸出的
* 格式:2.0.0
*/
private const val KEY_HARMONYOS_VERSION_NAME = "hw_sc.build.platform.version"
/**
* EMUI系統輸出的
* 格式:EmotionUI_8.0.0
*/
private const val KEY_EMUI_VERSION_NAME = "ro.build.version.emui"
/**
* MagicUI系統輸出的
* 格式:3.1.0
*/
private const val KEY_MAGICUI_VERSION = "ro.build.version.magic"
/**
* MIUI系統輸出的
* 格式:V12
*/
private const val KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"
/**
* OPPO手機ColorOS系統輸出的
* 格式:9
*/
private const val KEY_COLOROS_VERSION_NAME = "ro.build.version.opporom"
/**
* VIVO手機系統輸出的
* name格式:funtouch
* version格式: 9
*/
private const val KEY_VIVO_VERSION_NAME = "ro.vivo.os.name"
private const val KEY_VIVO_VERSION = "ro.vivo.os.version"
/**
* OonPlus手機系統輸出的
* 格式:Hydrogen OS 11.0.7.10.KB05
*/
private const val KEY_ONEPLUS_VERSION_NAME = "ro.rom.version"
/**
* 魅族手機系統輸出的
*/
private const val KEY_FLYME_VERSION_NAME = "ro.build.display.id"
/**
* nubia手機系統輸出的
*/
private const val KEY_NUBIA_VERSION_NAME = "ro.build.nubia.rom.name"
private const val KEY_NUBIA_VERSION_CODE = "ro.build.nubia.rom.code"
/**
* 傳入獲取手機系統屬性的key,可以得到rom系統版本信息
* @param key
* @return
*/
private fun getSystemPropertyValue(key: String): String {
val cmd = """
getprop "$key"
""".trimIndent()
val cmdResult = ShellUtils.execCmd(cmd, false)
return cmdResult.successMsg?:""
}
/**
* 判斷是否是華為鴻蒙系統,能否識別榮耀鴻蒙未知
*
* @return
*/
private fun isHarmonyOS(): Boolean {
val value = getHarmonyOsBrand()
return TextUtils.isEmpty(value).not()
}
private fun getHarmonyOsBrand(): String {
var result = ""
kotlin.runCatching {
val classType = Class.forName("com.huawei.system.BuildEx")
val getMethod = classType.getMethod("getOsBrand")
result = getMethod.invoke(classType) as String
}
return result
}
/**
* 通過手機品牌信息獲取手機rom系統+系統版本號
* @param phoneBrand
* @return 如 flyme 5.0
*/
fun getPhoneSystemName(): String {
if (TextUtils.isEmpty(customOS)) {
setCustomOSInfo()
}
return "$customOS $customOSVersion"
}
private val isMagicUI: Boolean = false
/**
* 通過手機品牌信息獲取手機rom系統
* @param phoneBrand
* @return
*/
fun getCustomOS(): String? {
if (TextUtils.isEmpty(customOS)) {
setCustomOSInfo()
}
return customOS
}
/**
* 通過手機品牌信息獲取手機rom系統版本號
* @param phoneBrand
* @return
*/
fun getCustomOSVersion(): String? {
if (TextUtils.isEmpty(customOS)) {
setCustomOSInfo()
}
return customOSVersion
}
/**
* 刪除字元串中的空格並全部轉成大寫
* @param str
* @return
*/
private fun deleteSpaceAndToUpperCase(str: String): String {
return if (TextUtils.isEmpty(str)) {
""
} else str.replace(" ".toRegex(), "").uppercase(Locale.getDefault())
}
private fun setCustomOSInfo() {
// 示例: xiaomi
val phoneBrand = Build.BRAND
try {
when (deleteSpaceAndToUpperCase(phoneBrand)) {
"HUAWEI" -> if (isHarmonyOS()) {
customOSVersion = getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME)
customOS = "Harmony"
} else {
customOS = "EMUI"
customOSVersion = getSystemPropertyValue(KEY_EMUI_VERSION_NAME)
}
"HONOR" -> if (isHarmonyOS()) {
customOS = "Harmony"
if (!TextUtils.isEmpty(getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME))) {
customOSVersion = getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME)
} else {
customOSVersion = ""
}
} else if (!TextUtils.isEmpty(getSystemPropertyValue(KEY_MAGICUI_VERSION))) {
customOS = "MagicUI"
customOSVersion = getSystemPropertyValue(KEY_MAGICUI_VERSION)
} else {
//格式:EmotionUI_8.0.0
customOS = "EMUI"
customOSVersion = getSystemPropertyValue(KEY_EMUI_VERSION_NAME)
}
"XIAOMI", "REDMI" -> {
//格式:MIUIV12
customOS = "MIUI"
customOSVersion = getSystemPropertyValue(KEY_MIUI_VERSION_NAME)
}
"REALME", "OPPO" -> {
//格式:ColorOSV2.1
customOS = "ColorOS"
customOSVersion = getSystemPropertyValue(KEY_COLOROS_VERSION_NAME)
}
"VIVO" -> {
val result = getSystemPropertyValue("ro.vivo.os.build.display.id")
if (result.isNotBlank()) {
//格式: OriginOS 1.0
val arr = result.split(" ")
customOS = arr.first()
customOSVersion = arr.last()
} else {
//格式:Funtouch9
customOS = "Funtouch"
customOSVersion = getSystemPropertyValue(KEY_VIVO_VERSION)
}
}
"ONEPLUS" -> {
//格式:Hydrogen OS 11.0.7.10.KB05
customOS = "Hydrogen"
customOSVersion = getSystemPropertyValue(KEY_ONEPLUS_VERSION_NAME)
}
"MEIZU" -> {
//格式:Flyme 6.3.5.1G
customOS = "Flyme"
customOSVersion = getSystemPropertyValue(KEY_FLYME_VERSION_NAME)
}
"NUBIA" -> {
//格式:nubiaUIV3.0
customOS = getSystemPropertyValue(KEY_NUBIA_VERSION_NAME)
customOSVersion = getSystemPropertyValue(KEY_NUBIA_VERSION_CODE)
}
else -> {
customOS = "Android"
customOSVersion = Build.VERSION.RELEASE
}
}
} catch (e: Exception) {
customOS = "Android"
customOSVersion = Build.VERSION.RELEASE
}
}
}
用法:
CustomOSUtils.getPhoneSystemName()
參考
- Android利用Java反射獲取用戶手機的rom定製系統及版本,EMUI,MIUI,ColorOS,FunthouchOS等_代碼獲取miui版本號-CSDN博客
- Android-ROM-Identifier/build_prop/中興-BA601 at d491963be4ccefffb33b0612283b5ec586e0a7e9 · SenhLinsh/Android-ROM-Identifier
- AndroidMonitor/roms at bd2bca4442492ab69cad2f82babc32da64672ac5 · jackuhan/AndroidMonitor
- HappyPlayer5/app/src/main/java/com/zlm/hp/utils/OSUtils.java at ebacd2332fa6991c40ca57a097ac9c9f791d5f01 · zhangliangming/HappyPlayer5
提問之前,請先看提問須知 點擊右側圖標發起提問 或者加入QQ群一起學習 TornadoFx學習交流群:1071184701