直接使用Android api获取到的设备名称,是手机的具体名称,但不是大家耳熟能详的设备“名字”,因此整理了下,各品牌获取的方法:
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DeviceInfoUtil {
public static String getMarketName(Context ctx) {
switch (Build.MANUFACTURER.toUpperCase()) {
case "HONOR":
case "HUAWEI":
return byHuawi(ctx);
case "XIAOMI":
return byXiaoMi(ctx);
case "OPPO":
return byOppo(ctx);
case "IQOO":
case "VIVO":
return byVivo(ctx);
case "REALME":
return byRealme(ctx);
}
return commonMarketName(ctx);
}
public static String getCpuName() {
switch (Build.MANUFACTURER.toUpperCase()) {
case "HONOR":
case "HUAWEI":
return byHuawiCpu();
}
return Build.HARDWARE;
}
private static String byHuawiCpu() {
String name = getPropName("ro.product.vendor.model");
if (!TextUtils.isEmpty(name)) {
return name;
}
return Build.HARDWARE;
}
private static String byHuawi(Context ctx) {
String name = getPropName("ro.config.marketing_name");
if (!TextUtils.isEmpty(name)) {
return name;
}
return commonMarketName(ctx);
}
private static String byRealme(Context ctx) {
String name1 = getPropName("ro.oppo.market.enname");
if (!TextUtils.isEmpty(name1)) {
return name1;
}
String name2 = getPropName("ro.vendor.oplus.market.enname");
if (!TextUtils.isEmpty(name2)) {
return name2;
}
return commonMarketName(ctx);
}
private static String byVivo(Context ctx) {
String name1 = getPropName("ro.vendor.vivo.market.name");
if (!TextUtils.isEmpty(name1)) {
return name1;
}
String name2 = getPropName("ro.vivo.market.name");
if (!TextUtils.isEmpty(name2)) {
return name2;
}
return commonMarketName(ctx);
}
private static String byOppo(Context ctx) {
String name = getPropName("ro.vendor.oplus.market.name");
if (!TextUtils.isEmpty(name)) {
return name;
}
return commonMarketName(ctx);
}
private static String byXiaoMi(Context ctx) {
String name1 = getPropName("ro.product.marketname");
if (!TextUtils.isEmpty(name1)) {
return name1;
}
String name2 = getPropName("ro.product.odm.marketname");
if (!TextUtils.isEmpty(name2)) {
return name2;
}
String name3 = getPropName("ro.product.vendor.marketname");
if (!TextUtils.isEmpty(name3)) {
return name3;
}
return commonMarketName(ctx);
}
private static String commonMarketName(Context ctx) {
String name4 = Settings.Secure.getString(ctx.getContentResolver(), "bluetooth_name");
if (!TextUtils.isEmpty(name4)) {
return name4;
}
return Build.MODEL;
}
private static String getPropName(String propName) {
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
String line = input.readLine();
if (!TextUtils.isEmpty(line)) {
return line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
网友评论