原文: Android 获取设备的CPU型号和设备型号-Stars-One的杂货小窝
之前整的项目的总结信息,可能不太全,凑合着用吧,代码在最下面一节
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值文章来源:https://www.toymoban.com/news/detail-838593.html
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
}
}
}
用法:文章来源地址https://www.toymoban.com/news/detail-838593.html
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
到了这里,关于Android 获取设备的CPU型号和设备型号的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!