仅在设备而非模拟器上运行Android应用 [英] Run Android app on only on device not emulator
问题描述
如何仅在设备上运行Android应用 。
在应用启动时检查它是否在设备或模拟器上启动。如果它是模拟器想要停止应用程序。
when app start check it launch on device or emulator. If it is emulator wanna 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屋!