[英]Run Android app Only on Physical Device not Emulator
How can I make my app run only on a physical android device, not an emulator.
如何让我的应用程序仅在物理 android 设备上运行,而不是在模拟器上运行。
When the app starts I want to check if the device is a physical device or emulator.当应用程序启动时,我想检查设备是物理设备还是模拟器。 If it is an emulator, I want my app to stop.
如果它是模拟器,我希望我的应用程序停止。
How can I do this?我怎样才能做到这一点?
In the onCreate()
method of your launch activity, you can check whether the device is running on an emulator and, if it is, just call finish()
.在启动活动的
onCreate()
方法中,您可以检查设备是否正在模拟器上运行,如果是,只需调用finish()
。 To check whether you're running on an emulator, you can use something like the following code (taken from this answer ):要检查您是否在模拟器上运行,您可以使用类似以下代码的内容(取自此答案):
public static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| "google_sdk".equals(Build.PRODUCT);
}
You can find lots of other suggestions on the web for detecting an emulator environment.您可以在网络上找到许多其他建议来检测模拟器环境。 I don't know of any that are absolutely foolproof, but the above is pretty robust.
我不知道有什么绝对万无一失的,但上面的内容非常强大。
You can try something like below您可以尝试以下操作
boolean isEmulator() {
return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.HARDWARE.equals("vbox86")
|| Build.HARDWARE.toLowerCase().contains("nox")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MODEL.toLowerCase().contains("droid4x")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("sdk_google")
|| Build.PRODUCT.contains("google_sdk")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("sdk_x86")
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator")
|| Build.PRODUCT.toLowerCase().contains("nox")
|| Build.BOARD.toLowerCase().contains("nox")
|| Build.BOOTLOADER.toLowerCase().contains("nox")
|| Build.SERIAL.toLowerCase().contains("nox");
}
It's an update of the code used in the Flutter project(device info plugin).它是 Flutter 项目(设备信息插件)中使用的代码的更新。 check it here
在这里检查
You can also use code below, It worked for me and the code is very less: 您还可以在下面使用代码,它对我有用,并且代码很少:
public static final String DSN_EMULATOR = "unknown";
if(Build.SERIAL.equalsIgnoreCase(AppConstant.DSN_EMULATOR) || Build.SERIAL.contains("EMULATOR")) {
// It's an emulator
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.