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

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

    I just look for _sdk, _sdk_ or sdk_, or even just sdk part in Build.PRODUCT:

    if(Build.PRODUCT.matches(".*_?sdk_?.*")){
      //-- emulator --
    }else{
      //-- other device --
    }
    
    0 讨论(0)
  • 2020-11-22 17:39

    I never found a good way to tell if you're in the emulator.

    but if you just need to detecet if you're in a development environment you can do this :

         if(Debug.isDebuggerConnected() ) {
            // Things to do in debug environment...
        }
    

    Hope this help....

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

    The above suggested solution to check for the ANDROID_ID worked for me until I updated today to the latest SDK tools released with Android 2.2.

    Therefore I currently switched to the following solution which works so far with the disadvantage however that you need to put the PHONE_STATE read permission (<uses-permission android:name="android.permission.READ_PHONE_STATE"/>)

    private void checkForDebugMode() {
        ISDEBUGMODE = false; //(Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID) == null);
    
        TelephonyManager man = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        if(man != null){
            String devId = man.getDeviceSoftwareVersion();
            ISDEBUGMODE = (devId == null);
        }
    } 
    
    0 讨论(0)
  • 2020-11-22 17:41

    Actually, ANDROID_ID on 2.2 always equals 9774D56D682E549C (according to this thread + my own experiments).

    So, you could check something like this:

    String androidID = ...;
    if(androidID == null || androidID.equals("9774D56D682E549C"))
        do stuff;
    

    Not the prettiest, but it does the job.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-22 17:43
    Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")
    

    This should return true if the app is running on an emulator.

    What we should be careful about is not detecting all the emulators because there are only several different emulators. It is easy to check. We have to make sure that actual devices are not detected as an emulator.

    I used the app called "Android Device Info Share" to check this.

    On this app, you can see various kinds of information of many devices (probably most devices in the world; if the device you are using is missing from the list, it will be added automatically).

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