How can I detect when an Android application is running in the emulator?

前端 未结 30 2321
无人及你
无人及你 2020-11-22 16:42

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

相关标签:
30条回答
  • 2020-11-22 17:16

    Both the following are set to "google_sdk":

    Build.PRODUCT
    Build.MODEL
    

    So it should be enough to use either one of the following lines.

    "google_sdk".equals(Build.MODEL)
    

    or

    "google_sdk".equals(Build.PRODUCT)
    
    0 讨论(0)
  • 2020-11-22 17:17

    use this function :

     public static final boolean isEmulator() {
    
        int rating = 0;
    
        if ((Build.PRODUCT.equals("sdk")) || (Build.PRODUCT.equals("google_sdk"))
                || (Build.PRODUCT.equals("sdk_x86")) || (Build.PRODUCT.equals("vbox86p"))) {
            rating++;
        }
        if ((Build.MANUFACTURER.equals("unknown")) || (Build.MANUFACTURER.equals("Genymotion"))) {
            rating++;
        }
        if ((Build.BRAND.equals("generic")) || (Build.BRAND.equals("generic_x86"))) {
            rating++;
        }
        if ((Build.DEVICE.equals("generic")) || (Build.DEVICE.equals("generic_x86")) || (Build.DEVICE.equals("vbox86p"))) {
            rating++;
        }
        if ((Build.MODEL.equals("sdk")) || (Build.MODEL.equals("google_sdk"))
                || (Build.MODEL.equals("Android SDK built for x86"))) {
            rating++;
        }
        if ((Build.HARDWARE.equals("goldfish")) || (Build.HARDWARE.equals("vbox86"))) {
            rating++;
        }
        if ((Build.FINGERPRINT.contains("generic/sdk/generic"))
                || (Build.FINGERPRINT.contains("generic_x86/sdk_x86/generic_x86"))
                || (Build.FINGERPRINT.contains("generic/google_sdk/generic"))
                || (Build.FINGERPRINT.contains("generic/vbox86p/vbox86p"))) {
            rating++;
        }
    
        return rating > 4;
    
        }
    
    0 讨论(0)
  • 2020-11-22 17:17

    Don't know if there are better ways to detect the emu, but the emulator will have the file init.goldfish.rc in the root-directory.

    It's the emulator specific startup-script, and it shouldn't be there on a non-emulator build.

    0 讨论(0)
  • 2020-11-22 17:19

    Put a file in the file system of the emulator; since the file won't exist on the real device, this should be stable, reliable and easy to fix when it breaks.

    0 讨论(0)
  • 2020-11-22 17:20

    Based on hints from other answers, this is probably the most robust way:

    isEmulator = "goldfish".equals(Build.HARDWARE)

    0 讨论(0)
  • 2020-11-22 17:20

    This code works for me

    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = tm.getNetworkOperatorName();
    if("Android".equals(networkOperator)) {
        // Emulator
    }
    else {
        // Device
    }
    

    In case that device does not have sim card, It retuns empty string:""

    Since Android emulator always retuns "Android" as network operator, I use above code.

    0 讨论(0)
提交回复
热议问题