How can I make my app run only on a physical android device, not an emulator.
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?
How can I make my app run only on a physical android device, not an emulator.
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()
. 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). check it here