如何获取Android设备名称?我正在使用HTC欲望。当我通过HTC Sync连接它时,软件显示“HTC Smith”的名称。我想通过代码获取这个名称。
这在Android中是如何实现的呢?
如何获取Android设备名称?我正在使用HTC欲望。当我通过HTC Sync连接它时,软件显示“HTC Smith”的名称。我想通过代码获取这个名称。
这在Android中是如何实现的呢?
当前回答
简单地使用
BluetoothAdapter.getDefaultAdapter () . getname ()
其他回答
在Android的GNU/Linux环境中,例如,通过非根设备上的Termux UNIX shell,它可以通过/system/bin/getprop命令获得,而每个值的含义都在Android中的Build.java中解释(也在googlesource中):
% /system/bin/getprop | fgrep ro.product | tail -3
[ro.product.manufacturer]: [Google]
[ro.product.model]: [Pixel 2 XL]
[ro.product.name]: [taimen]
% /system/bin/getprop ro.product.model
Pixel 2 XL
% /system/bin/getprop ro.product.model | tr ' ' _
Pixel_2_XL
例如,它可以被设置为tmux中status-right的pane_title,如下所示:
tmux select-pane "$(getprop ro.product.model)"
以下对我有用。
String deviceName = Settings.Global.getString(.getContentResolver(), Settings.Global.DEVICE_NAME);
我不认为这是重复的答案。上面的ppl正在谈论设置安全,为我设置安全是给空,如果我使用设置全局它工作。不管怎样,谢谢你。
尝试了这些库,但没有按照我的期望工作,并给出了绝对错误的名称。
所以我自己用同样的数据创建了这个库。 这是链接
AndroidPhoneNamesFinder
要使用这个库,只需添加这个来实现
implementation 'com.github.aishik212:AndroidPhoneNamesFinder:v1.0.2'
然后使用下面的kotlin代码
DeviceNameFinder.getPhoneValues(this, object : DeviceDetailsListener
{
override fun details(doQuery: DeviceDetailsModel?)
{
super.details(doQuery)
Log.d(TAG, "details: "+doQuery?.calculatedName)
}
})
这些是您将从DeviceDetailsModel中获得的值
val brand: String? #This is the brandName of the Device
val commonName: String?, #This is the most common Name of the Device
val codeName: String?, #This is the codeName of the Device
val modelName: String?, #This is the another uncommon Name of the Device
val calculatedName: String?, #This is the special name that this library tries to create from the above data.
Android模拟器示例-
brand=Google
commonName=Google Android Emulator
codeName=generic_x86_arm
modelName=sdk_gphone_x86
calculatedName=Google Android Emulator
static String getDeviceName() {
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method getMethod = systemPropertiesClass.getMethod("get", String.class);
Object object = new Object();
Object obj = getMethod.invoke(object, "ro.product.device");
return (obj == null ? "" : (String) obj);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
你可以通过这种方式得到'idol3'。
试试这段代码。你会得到android设备名。
public static String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return model;
}
return manufacturer + " " + model;
}