如何检测 Android 应用程序何时在模拟器中运行? [英] How can I detect when an Android application is running in the emulator?

查看:73
本文介绍了如何检测 Android 应用程序何时在模拟器中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码在模拟器上运行时与在设备上运行时略有不同.(例如,使用 10.0.2.2 而不是公共 URL 来自动针对开发服务器运行.)检测 Android 应用程序何时在模拟器中运行的最佳方法是什么?

I would like to have my code run slightly differently when running on the emulator than when running on a device. (For example, using 10.0.2.2 instead of a public URL to run against a development server automatically.) What is the best way to detect when an Android application is running in the emulator?

推荐答案

这个方案怎么样(SystemProperties的类实现可用此处):

How about this solution (class implementation of SystemProperties is available here):

private var sIsProbablyRunningOnEmulator: Boolean? = null

fun isProbablyRunningOnEmulator(): Boolean {
    var result = sIsProbablyRunningOnEmulator
    if (result != null)
        return result
    // Android SDK emulator
    result = (Build.FINGERPRINT.startsWith("google/sdk_gphone_")
            && Build.FINGERPRINT.endsWith(":user/release-keys")
            && Build.MANUFACTURER == "Google" && Build.PRODUCT.startsWith("sdk_gphone_") && Build.BRAND == "google"
            && Build.MODEL.startsWith("sdk_gphone_"))
            //
            || 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")
            //bluestacks
            || "QC_Reference_Phone" == Build.BOARD && !"Xiaomi".equals(Build.MANUFACTURER, ignoreCase = true) //bluestacks
            || Build.MANUFACTURER.contains("Genymotion")
            || Build.HOST=="Build2" //MSI App Player
            || Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")
            || Build.PRODUCT == "google_sdk"
            // another Android SDK emulator check
            || SystemProperties.getProp("ro.kernel.qemu") == "1"
    sIsProbablyRunningOnEmulator = result
    return result
}

请注意,某些模拟器会伪造真实设备的确切规格,因此可能无法检测到它.我已经添加了我能添加的内容,但我认为没有 100% 的方法可以检测它是否真的是模拟器.

Note that some emulators fake exact specs of real devices, so it might be impossible to detect it. I've added what I could, but I don't think there is a 100% way to detect if it's really an emulator or not.

这里有一个小片段,您可以在 APK 中制作以显示有关它的各种内容,因此您可以添加自己的规则:

Here a tiny snippet you can make in the APK to show various things about it, so you could add your own rules:

        textView.text = "FINGERPRINT:${Build.FINGERPRINT}
" +
                "MODEL:${Build.MODEL}
" +
                "MANUFACTURER:${Build.MANUFACTURER}
" +
                "BRAND:${Build.BRAND}
" +
                "DEVICE:${Build.DEVICE}
" +
                "BOARD:${Build.BOARD}
" +
                "HOST:${Build.HOST}
" +
                "PRODUCT:${Build.PRODUCT}
"

这篇关于如何检测 Android 应用程序何时在模拟器中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆