我想知道是否有一种在Android中以编程方式阅读手机模型的方法。

我想要一个像HTC梦想,里程碑,蓝宝石或任何字符串…


当前回答

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

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

其他回答

Kotlin简写版:

import android.os.Build.MANUFACTURER
import android.os.Build.MODEL

fun getDeviceName(): String =
    if (MODEL.startsWith(MANUFACTURER, ignoreCase = true)) {
        MODEL
    } else {
        "$MANUFACTURER $MODEL"
    }.capitalize(Locale.ROOT)

是的:Build.MODEL。

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

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

方法获取电话设备名称

BluetoothAdapter

如果手机不支持蓝牙,那么您必须从构造设备名称

android.os.Build类

下面是获取电话设备名称的示例代码。

public String getPhoneDeviceName() {  
        String name=null;
        // Try to take Bluetooth name
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            name = adapter.getName();
        }

        // If not found, use MODEL name
        if (TextUtils.isEmpty(name)) {
            String manufacturer = Build.MANUFACTURER;
            String model = Build.MODEL;
            if (model.startsWith(manufacturer)) {
                name = model;
            } else {
                name = manufacturer + " " + model;
            }
        } 
        return name;
}

芬兰湾的科特林版本

val Name: String by lazy {
    val manufacturer = Build.MANUFACTURER
    val model = Build.MODEL
    if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
        capitalize(model)
    } else {
        capitalize(manufacturer) + " " + model
    }
}

private fun capitalize(s: String?): String {
    if (s == null || s.isEmpty()) {
        return ""
    }
    val first = s[0]
    return if (Character.isUpperCase(first)) {
        s
    } else {
        Character.toUpperCase(first) + s.substring(1)
    }
}