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

前端 未结 30 2743
无人及你
无人及你 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:21

    Checking the answers, none of them worked when using LeapDroid, Droid4x or Andy emulators,

    What does work for all cases is the following:

     private static String getSystemProperty(String name) throws Exception {
        Class systemPropertyClazz = Class.forName("android.os.SystemProperties");
        return (String) systemPropertyClazz.getMethod("get", new Class[]{String.class}).invoke(systemPropertyClazz, new Object[]{name});
    }
    
    public boolean isEmulator() {
        boolean goldfish = getSystemProperty("ro.hardware").contains("goldfish");
        boolean emu = getSystemProperty("ro.kernel.qemu").length() > 0;
        boolean sdk = getSystemProperty("ro.product.model").equals("sdk");
        return goldfish || emu || sdk;
    }
    

提交回复
热议问题