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

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

    you can check the IMEI #, http://developer.android.com/reference/android/telephony/TelephonyManager.html#getDeviceId%28%29

    if i recall on the emulator this return 0. however, there's no documentation i can find that guarantees that. although the emulator might not always return 0, it seems pretty safe that a registered phone would not return 0. what would happen on a non-phone android device, or one without a SIM card installed or one that isn't currently registered on the network?

    seems like that'd be a bad idea, to depend on that.

    it also means you'd need to ask for permission to read the phone state, which is bad if you don't already require it for something else.

    if not that, then there's always flipping some bit somewhere before you finally generate your signed app.

提交回复
热议问题