当前位置:主页 > 软件编程 > JAVA代码 >

Android 判断真机和模拟器的方法

时间:2021-02-19 15:17:30 | 栏目:JAVA代码 | 点击:

 Android 判断真机和模拟器的方法

最近有一些业务需求要判断是否在真机上运行还是在模拟器上运行两种不同的情况下做不同的业务逻辑操作。上网查了查还真有不少的资源。

接下来给大家展示下实例代码:

private static String getSystemProperty(String name) throws Exception {
  Class systemPropertyClazz = Class.forName("android.os.SystemProperties");
  return (String) systemPropertyClazz.getMethod("get", new Class[]{String.class})
      .invoke(systemPropertyClazz, new Object[]{name});
}

public static boolean checkEmulator() {
  try {
    boolean goldfish = getSystemProperty("ro.hardware").contains("goldfish");
    boolean emu = getSystemProperty("ro.kernel.qemu").length() > 0;
    boolean sdk = getSystemProperty("ro.product.model").equals("sdk");
    if (emu || goldfish || sdk) {
      return true;
    }
  } catch (Exception e) {
  }
  return false;
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

相关文章