且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅在设备而非模拟器上运行Android应用

更新时间:2023-01-24 21:06:07

在启动活动的 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.