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 public URL to run against a development server automatically.) What is the best way to detect when an Android application is running in the emulator?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I just look for
_sdk
,_sdk_
orsdk_
, or even justsdk
part inBuild.PRODUCT
:Put a file in the file system of the emulator; since the file won't exist on the real device, this should be stable, reliable and easy to fix when it breaks.
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.
Whichever code you use to do emulator detection, I'd highly recommend writing unit tests to cover all the
Build.FINGERPRINT
,Build.HARDWARE
andBuild.MANUFACTURER
values that you are depending on. Here are some example tests:...and here's our code (debug logs and comments removed for conciseness):
How about something like the code below to tell if your app was signed with the debug key? it's not detecting the emulator but it might work for your purpose?
Don't know if there are better ways to detect the emu, but the emulator will have the file
init.goldfish.rc
in the root-directory.It's the emulator specific startup-script, and it shouldn't be there on a non-emulator build.