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

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

    I've collected all the answers on this question and came up with function to detect if Android is running on a vm/emulator:

    public boolean isvm(){
    
    
            StringBuilder deviceInfo = new StringBuilder();
            deviceInfo.append("Build.PRODUCT " +Build.PRODUCT +"\n");
            deviceInfo.append("Build.FINGERPRINT " +Build.FINGERPRINT+"\n");
            deviceInfo.append("Build.MANUFACTURER " +Build.MANUFACTURER+"\n");
            deviceInfo.append("Build.MODEL " +Build.MODEL+"\n");
            deviceInfo.append("Build.BRAND " +Build.BRAND+"\n");
            deviceInfo.append("Build.DEVICE " +Build.DEVICE+"\n");
            String info = deviceInfo.toString();
    
    
            Log.i("LOB", info);
    
    
            Boolean isvm = false;
            if(
                    "google_sdk".equals(Build.PRODUCT) ||
                    "sdk_google_phone_x86".equals(Build.PRODUCT) ||
                    "sdk".equals(Build.PRODUCT) ||
                    "sdk_x86".equals(Build.PRODUCT) ||
                    "vbox86p".equals(Build.PRODUCT) ||
                    Build.FINGERPRINT.contains("generic") ||
                    Build.MANUFACTURER.contains("Genymotion") ||
                    Build.MODEL.contains("Emulator") ||
                    Build.MODEL.contains("Android SDK built for x86")
                    ){
                isvm =  true;
            }
    
    
            if(Build.BRAND.contains("generic")&&Build.DEVICE.contains("generic")){
                isvm =  true;
            }
    
            return isvm;
        }
    

    Tested on Emulator, Genymotion and Bluestacks (1 October 2015).

提交回复
热议问题