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

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

    I found the new emulator Build.HARDWARE = "ranchu".

    Reference:https://groups.google.com/forum/#!topic/android-emulator-dev/dltBnUW_HzU

    And also I found the Android official way to check whether emulator or not.I think it's good reference for us.

    Since Android API Level 23 [Android 6.0]

    package com.android.internal.util;
    
    /**
     * @hide
     */
    public class ScreenShapeHelper {
        private static final boolean IS_EMULATOR = Build.HARDWARE.contains("goldfish");
    }
    

    We have ScreenShapeHelper.IS_EMULATOR to check whether emulator.

    Since Android API Level 24 [Android 7.0]

    package android.os;
    
    /**
     * Information about the current build, extracted from system properties.
     */
    public class Build {
    
    
        /**
         * Whether this build was for an emulator device.
         * @hide
         */
        public static final boolean IS_EMULATOR = getString("ro.kernel.qemu").equals("1");
    
    }
    

    We have Build.IS_EMULATOR to check whether emulator.

    The way the official to check whether emulator is not new,and also maybe not enough,the answers above also mentioned.

    But this maybe show us that the official will provide the way of official to check whether emulator or not.

    As using the above all ways mentioned,right now we can also use the two ways about to check whether emulator.

    How to access the com.android.internal package and @hide

    and wait for the official open SDK.

提交回复
热议问题