查看源代码com.android.ddmlib.Device以了解 DDMS 如何生成设备名称/序列号:
private static final String DEVICE_MODEL_PROPERTY = "ro.product.model"; //$NON-NLS-1$
private static final String DEVICE_MANUFACTURER_PROPERTY = "ro.product.manufacturer"; //$NON-NLS-1$
... ...
private static final char SEPARATOR = '-';
... ...
@Override
public String getName() {
if (mName == null) {
mName = constructName();
}
return mName;
}
private String constructName() {
if (isEmulator()) {
String avdName = getAvdName();
if (avdName != null) {
return String.format("%s [%s]", avdName, getSerialNumber());
} else {
return getSerialNumber();
}
} else {
String manufacturer = cleanupStringForDisplay(
getProperty(DEVICE_MANUFACTURER_PROPERTY));
String model = cleanupStringForDisplay(
getProperty(DEVICE_MODEL_PROPERTY));
StringBuilder sb = new StringBuilder(20);
if (manufacturer != null) {
sb.append(manufacturer);
sb.append(SEPARATOR);
}
if (model != null) {
sb.append(model);
sb.append(SEPARATOR);
}
sb.append(getSerialNumber());
return sb.toString();
}
}
private String cleanupStringForDisplay(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetterOrDigit(c)) {
sb.append(Character.toLowerCase(c));
} else {
sb.append('_');
}
}
return sb.toString();
}
如果您想查看 DDMS 如何呈现此设备名称/序列号,请参阅com.android.ddmuilib.DevicePanel。
ro.product.manufacturer和ro.product.model在/system/build.prop中,您可以使用adb -e shell getprop|grep "\[ro.product"
查看当前值:
[ro.product.manufacturer]: [samsung]
[ro.product.model]: [GT-I9100]
那么 DDMS 透视图中显示的设备名称/序列号是samsung-gt_i9100-0x0123456789ABCDEF。请注意,一些杂牌供应商没有正确设置这两个属性,这就是为什么对于这些设备,它只显示序列号。
Eclipse 中没有任何配置可以让您简单地勾选并强制显示它。如果您的设备已root,您可以编辑这些属性,以便设备的制造商和型号在DDMS 透视图中正确显示,例如,使用adb shell setprop <key> <value>
或直接编辑文件系统中的build.prop。
DDMS 深入
DDMS 获取设备信息的方式比较复杂,一般来说,AndroidDebugBridge 启动并运行时,会在单独的线程中启动一个 DeviceMonitor,不断监听设备连接,getprop
并向特定设备发出远程 shell 命令查询设备ro.product.manufacturer
和之类的信息ro.product.model
,这个远程shell命令执行不可靠(可能受多种因素影响),并且不能保证一直抓取属性。请参阅com.android.ddmlib.DeviceMonitor:
/**
* Queries a device for its build info.
* @param device the device to query.
*/
private void queryNewDeviceForInfo(Device device) {
// TODO: do this in a separate thread.
try {
// first get the list of properties.
device.executeShellCommand(GetPropReceiver.GETPROP_COMMAND,
new GetPropReceiver(device));
queryNewDeviceForMountingPoint(device, IDevice.MNT_EXTERNAL_STORAGE);
queryNewDeviceForMountingPoint(device, IDevice.MNT_DATA);
queryNewDeviceForMountingPoint(device, IDevice.MNT_ROOT);
// now get the emulator Virtual Device name (if applicable).
if (device.isEmulator()) {
EmulatorConsole console = EmulatorConsole.getConsole(device);
if (console != null) {
device.setAvdName(console.getAvdName());
}
}
} catch (TimeoutException e) {
Log.w("DeviceMonitor", String.format("Connection timeout getting info for device %s",
device.getSerialNumber()));
} catch (AdbCommandRejectedException e) {
// This should never happen as we only do this once the device is online.
Log.w("DeviceMonitor", String.format(
"Adb rejected command to get device %1$s info: %2$s",
device.getSerialNumber(), e.getMessage()));
} catch (ShellCommandUnresponsiveException e) {
Log.w("DeviceMonitor", String.format(
"Adb shell command took too long returning info for device %s",
device.getSerialNumber()));
} catch (IOException e) {
Log.w("DeviceMonitor", String.format(
"IO Error getting info for device %s",
device.getSerialNumber()));
}
}
注意device.executeShellCommand()
由 抛出和处理的所有异常DeviceMonitor.queryNewDeviceForInfo()
,如果发生任何这些异常,DDMS 将无法正确获取属性。
如果您想阅读完整的源代码,请查看此处。