模拟器中的设备ID

device id in emulators

是否可以仅通过设备ID信息来验证设备是真实的android设备还是模拟器?我读到如果你尝试在模拟器中获取值,你会得到 NULL,尽管我尝试过并且每次都得到不同的值。

String android_id = Settings.Secure.getString(this.getContentResolver(),
                Settings.Secure.ANDROID_ID);

您可以检查系统属性用户类型 在大多数模拟器中它将 return "userdebug" 而在普通设备上它将 return "user"

注意:在某些已获得 root 权限的设备上,它也可能 return "userdebug"

public static String getUserType() {
  String uType = "-"; 
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      uType = (String) get.invoke(c, "ro.build.type", "-");
  } catch (Exception ignored) {}
  return uType;
}