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

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

    This code works for me

    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = tm.getNetworkOperatorName();
    if("Android".equals(networkOperator)) {
        // Emulator
    }
    else {
        // Device
    }
    

    In case that device does not have sim card, It retuns empty string:""

    Since Android emulator always retuns "Android" as network operator, I use above code.

提交回复
热议问题