How to find out from code if my Android app runs on emulator or real device?

后端 未结 9 2235

I have read this stackoverflow thread already and I tried using the code given in that answer to find out if I run my code on the emulator or on a real device:



        
相关标签:
9条回答
  • 2020-12-09 17:52

    There's a rather old thread on Android Developers group that suggests checking the number of sensors on the device. Something like this might work:

    SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ALL).isEmpty()) {
        // running on an emulator
    } else {
        // running on a device
    }
    

    I haven't tried this, so I have no idea how reliable the suggestion is. (Perhaps some emulators now report some sensors; perhaps some devices report no sensors. [Is there an Android toothbrush yet?]) But it can't be worse than checking for a null ANDROID_ID (which doesn't work as of 2.2).

    P.S. Another thread claims that as of 2.2, the ANDROID_ID for the emulator is always "9774D56D682E549C". However, you are apparently getting some other hex string, so I don't think this is right, either.

    P.P.S. Other suggestions I haven't tried are here. One that seems particularly nice (if it works) is:

    if (android.os.Build.MODEL.equals(“google_sdk”)) {
       // emulator
    } else {
       //not emulator
    }
    
    0 讨论(0)
  • 2020-12-09 17:54

    With the advent of the new Intel native emulator the above mentioned methods did not work any longer. Now I am using this code snippet which works on both Intel and ARM emulators:

    if (Build.MODEL.contains("google_sdk") ||
        Build.MODEL.contains("Emulator") ||
        Build.MODEL.contains("Android SDK")) {
      RunsInEmulator = true;
    }
    
    0 讨论(0)
  • 2020-12-09 17:54

    As of writing this, nothing in this thread worked for the Bluestacks 4 emulator except trying to check for sensors. And so I checked the battery temperature using this gist. It should return 0.0 which means it does not have a battery temperature (and therefore it's an emulator).

    public float getCpuTemp() {
        Process process;
        try {
            process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
            process.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            float temp = Float.parseFloat(line) / 1000.0f;
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
            return 0.0f;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 17:58

    Following one is correctly detect my emulator

    if (Build.BRAND.equalsIgnoreCase("generic")) {
                  //"YES, I am an emulator"
           } else {
                  //"NO, I am NOT an emulator"
           }
    
    0 讨论(0)
  • 2020-12-09 18:00

    This is the standard google flutter emulator check :

    public boolean isEmulator() {
        return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.HARDWARE.contains("goldfish")
                || Build.HARDWARE.contains("ranchu")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || Build.PRODUCT.contains("sdk_google")
                || Build.PRODUCT.contains("google_sdk")
                || Build.PRODUCT.contains("sdk")
                || Build.PRODUCT.contains("sdk_x86")
                || Build.PRODUCT.contains("vbox86p")
                || Build.PRODUCT.contains("emulator")
                || Build.PRODUCT.contains("simulator");
    }
    
    0 讨论(0)
  • 2020-12-09 18:03

    This should do it:

    boolean inEmulator = false;
    String brand = Build.BRAND;
    if (brand.compareTo("generic") == 0)
    {
        inEmulator = true;
    }
    

    EDIT:

    boolean inEmulator = "generic".equals(Build.BRAND.toLowerCase());
    
    0 讨论(0)
提交回复
热议问题