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

后端 未结 9 2254

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
    }
    

提交回复
热议问题