views:

997

answers:

8

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?

A: 

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.

Nils Pipenbrinck
+1  A: 

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.

Jeff
+6  A: 

The value of ANDROID_ID will be null on the emulator, non-null on devices.

CommonsWare
apparently this doesn't work if you upgrade to Android 2.2 (SDK v 8). It worked before, but doesn't any more now...
Juri
A: 

Another option would be to look at the ro.hardware property and see if its set to goldfish. Unfortunately there doesn't seem to be an easy way to do this from Java but its trivial from C using property_get().

Tim Kryger
A: 

Might have a look at android.os.Build.

alex
+6  A: 

Well Android id does not work for me, I'm currently using:

"google_sdk".equals( Build.PRODUCT );
Marcus
Anyone reading this may be interested to know that this string appears to have changed to 'sdk', rather than 'google_sdk'.
Daniel
A: 

I don't have the rep here yet to downvote the top answer, but it is not correct that ANDROID_ID will be null under emulation: ANDROID_ID has the value "android_id" on my system's emulator, at least when running the Backflip profile.

DulcetTone
Similarly, Build.PRODUCT is "generic" under the Backflip's emulator add-on
DulcetTone
A: 

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);
    }
} 
Juri