仅在物理设备上而不是模拟器上运行Android应用 [英] Run Android app Only on Physical Device not Emulator
问题描述
如何使我的应用只能在物理android设备上运行,而不能在模拟器上运行.
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.
我该怎么做?
推荐答案
在启动活动的 onCreate()
方法中,您可以检查设备是否在模拟器上运行,以及是否在模拟器上运行是,只需调用 finish()
.要检查您是否在仿真器上运行,可以使用类似以下代码(取自此答案)的代码:
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.
这篇关于仅在物理设备上而不是模拟器上运行Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!