以编程方式获取 Android 手机型号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1995439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:06:52  来源:igfitidea点击:

Get Android Phone Model programmatically

androidandroid-hardware

提问by Andrea Baccega

I would like to know if there is a way for reading the Phone Model programmatically in Android.

我想知道是否有一种方法可以在 Android 中以编程方式读取电话模型。

I would like to get a string like HTC Dream, Milestone, Sapphire or whatever...

我想要一个像 HTC Dream、Milestone、Sapphire 或其他什么的字符串……

采纳答案by Jared Rummler

On many popular devices the market name of the device is not available. For example, on the Samsung Galaxy S6 the value of Build.MODELcould be "SM-G920F", "SM-G920I", or "SM-G920W8".

在许多流行的设备上,设备的市场名称不可用。例如,在三星Galaxy S6的价值Build.MODEL可能是"SM-G920F""SM-G920I""SM-G920W8"

I created a small library that gets the market (consumer friendly) name of a device. It gets the correct name for over 10,000devices and is constantly updated. If you wish to use my library click the link below:

我创建了一个小型库,用于获取设备的市场(消费者友好)名称。它为超过 10,000 台设备获取正确名称,并不断更新。如果您想使用我的图书馆,请单击以下链接:

AndroidDeviceNames Library on Github

Github 上的 AndroidDeviceNames 库



If you do not want to use the library above, then this is the best solution for getting a consumer friendly device name:

如果您不想使用上面的库,那么这是获得消费者友好设备名称的最佳解决方案:

/** Returns the consumer friendly device name */
public static String getDeviceName() {
  String manufacturer = Build.MANUFACTURER;
  String model = Build.MODEL;
  if (model.startsWith(manufacturer)) {
    return capitalize(model);
  }
  return capitalize(manufacturer) + " " + model;
}

private static String capitalize(String str) {
  if (TextUtils.isEmpty(str)) {
    return str;
  }
  char[] arr = str.toCharArray();
  boolean capitalizeNext = true;

  StringBuilder phrase = new StringBuilder();
  for (char c : arr) {
    if (capitalizeNext && Character.isLetter(c)) {
      phrase.append(Character.toUpperCase(c));
      capitalizeNext = false;
      continue;
    } else if (Character.isWhitespace(c)) {
      capitalizeNext = true;
    }
    phrase.append(c);
  }

  return phrase.toString();
}


Example from my Verizon HTC One M8:

来自我的 Verizon HTC One M8 的示例:

// using method from above
System.out.println(getDeviceName());
// Using https://github.com/jaredrummler/AndroidDeviceNames
System.out.println(DeviceName.getDeviceName());

Result:

结果:

HTC6525LVW

HTC One (M8)

HTC6525LVW

HTC 一号 (M8)

回答by Idolon

I use the following code to get the full device name. It gets model and manufacturer strings and concatenates them unless model string already contains manufacturer name (on some phones it does):

我使用以下代码来获取完整的设备名称。它获取型号和制造商字符串并将它们连接起来,除非型号字符串已经包含制造商名称(在某些手机上确实如此):

public String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
        return capitalize(model);
    } else {
        return capitalize(manufacturer) + " " + model;
    }
}


private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    if (Character.isUpperCase(first)) {
        return s;
    } else {
        return Character.toUpperCase(first) + s.substring(1);
    }
} 

 

 

Here are a few examples of device names I got from the users:

以下是我从用户那里获得的一些设备名称示例:

Samsung GT-S5830L
Motorola MB860
Sony Ericsson LT18i
LGE LG-P500
HTC Desire V
HTC Wildfire S A510e

三星 GT-S5830L
摩托罗拉 MB860
索尼爱立信 LT18i
LGE LG-P500
HTC Desire V
HTC Wildfire S A510e

回答by Mirko N.

Yes: Build.MODEL.

是:Build.MODEL

回答by Falcon165o

Actually that is not 100% correct. That can give you Model (sometime numbers).
Will get you the Manufacturer of the phone (HTC portion of your request):

实际上,这不是 100% 正确的。那可以给你模型(有时是数字)。
将为您提供手机制造商(您请求的 HTC 部分):

 Build.MANUFACTURER

For a product name:

对于产品名称:

 Build.PRODUCT

回答by ruX

For whom who looking for full list of properties of Buildhere is an example for Sony Z1 Compact:

对于寻找完整属性列表的人来说,Build这里以 Sony Z1 Compact 为例:

Build.BOARD = MSM8974
Build.BOOTLOADER = s1
Build.BRAND = Sony
Build.CPU_ABI = armeabi-v7a
Build.CPU_ABI2 = armeabi
Build.DEVICE = D5503
Build.DISPLAY = 14.6.A.1.236
Build.FINGERPRINT = Sony/D5503/D5503:5.1.1/14.6.A.1.236/2031203XXX:user/release-keys
Build.HARDWARE = qcom
Build.HOST = BuildHost
Build.ID = 14.6.A.1.236
Build.IS_DEBUGGABLE = false
Build.MANUFACTURER = Sony
Build.MODEL = D5503
Build.PRODUCT = D5503
Build.RADIO = unknown
Build.SERIAL = CB5A1YGVMT
Build.SUPPORTED_32_BIT_ABIS = [Ljava.lang.String;@3dd90541
Build.SUPPORTED_64_BIT_ABIS = [Ljava.lang.String;@1da4fc3
Build.SUPPORTED_ABIS = [Ljava.lang.String;@525f635
Build.TAGS = release-keys
Build.TIME = 144792559XXXX
Build.TYPE = user
Build.UNKNOWN = unknown
Build.USER = BuildUser

You can easily list those properties for your device in debug mode using "evaluate expression" dialog using kotlin:

您可以使用 kotlin 使用“评估表达式”对话框在调试模式下轻松列出设备的这些属性:

android.os.Build::class.java.fields.map { "Build.${it.name} = ${it.get(it.name)}"}.joinToString("\n")

回答by Ajay Pandya

String deviceName = android.os.Build.MODEL; // returns model name 

String deviceManufacturer = android.os.Build.MANUFACTURER; // returns manufacturer

Use Following method to get model & manufacturer programmatically

使用以下方法以编程方式获取型号和制造商

  public String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
            return capitalize(model);
        } else {
            return capitalize(manufacturer) + " " + model;
        }
    }


    private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    if (Character.isUpperCase(first)) {
        return s;
    } else {
        return Character.toUpperCase(first) + s.substring(1);
    }

回答by Edwin Evans

Apparently you need to use the list from Google at https://support.google.com/googleplay/answer/1727131

显然,您需要在https://support.google.com/googleplay/answer/1727131上使用来自 Google 的列表

The APIs don't return anything I expect or anything in Settings. For my Motorola X this is what I get

API 不会返回我期望的任何内容或设置中的任何内容。对于我的摩托罗拉 X,这就是我得到的

   Build.MODEL = "XT1053"
   Build.BRAND = "motorola"
   Build.PRODUCT = "ghost"

Going to the pagementioned above "ghost" maps to Moto X. Seems like this could be a tad simpler...

转到上面提到的页面,“ghost”映射到 Moto X。似乎这可能更简单一些......

回答by silkfire

The following strings are all of use when you want to retrieve manufacturer, name of the device, and/or the model:

当您想要检索制造商、设备名称和/或型号时,以下字符串都是有用的:

String manufacturer = Build.MANUFACTURER;
String brand        = Build.BRAND;
String product      = Build.PRODUCT;
String model        = Build.MODEL;

回答by RamakanthReddy

you can use the following code for getting the brand name and brand model of the device.

您可以使用以下代码获取设备的品牌名称和品牌型号。

 String brand = Build.BRAND; // for getting BrandName
 String model = Build.MODEL; // for getting Model of the device

回答by Saurabh Gaddelpalliwar

Here is my code , To get Manufacturer,Brand name,Os version and support API Level

这是我的代码,获取制造商、品牌名称、操作系统版本和支持 API 级别

String manufacturer = Build.MANUFACTURER;

String model = Build.MODEL + " " + android.os.Build.BRAND +" ("
           + android.os.Build.VERSION.RELEASE+")"
           + " API-" + android.os.Build.VERSION.SDK_INT;

if (model.startsWith(manufacturer)) {
    return capitalize(model);
} else {
    return capitalize(manufacturer) + " " + model;
}

Output:

输出:

System.out: button press on device name = Lava Alfa L iris(5.0) API-21