GideonKain
GideonKain

Reputation: 744

android is it possible for an app to detect when it is running on emulator vs device

I develop a game that is heavily dependent upon timing, when I run it in the emulator it executes understandably slower than when I run it on my phone. This forces me to up all the "stats" in my game, so that I can actually "beat it" when I am developing - when Debugging, the game is unwinnable.

Is there a call, or variable, or something that I can use to determine whether I am currently running on the Emulator and not a device.

I've considered trying to detect if Framerate is low. I've considered trying to read the "device name" from some sort of build in system field.

But neither seems like a very good method to pursue.

Any help would be great.

Upvotes: 2

Views: 1786

Answers (4)

Felix
Felix

Reputation: 89626

Dan S gave a good answer about how to detect when running on the emulator. However, a few tips:

  1. Instead of relying on something in the SDK, why not just set up your own flag? Just keep a public final static boolean isEmulator that you change from true to false depending on the environment, and build your code with ifs and elses around it. The Build.DEVICE method is not 100% safe, since some rooted devices might have that borked.
  2. Low framerate detection could be a good thing to implement. Given the wide range of Android devices, it might prove helpful on lower-end ones.

Upvotes: 0

Avram Score
Avram Score

Reputation: 4095

If you're using Google APIs, you want:

"google_sdk".equals( Build.PRODUCT );

If not, you'll want to use:

"sdk".equals( Build.PRODUCT );

The older (since deprecated) way to do this was to check ANDROID_ID, which is null on the AVD, but this doesn't work on API 7 and above:

// ONLY WORKS ON 2.1 AND BELOW

 String android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID); 
  if (android_id == null) { 
      // Emulator!  
  } else { 
      // Device
  }  

Upvotes: 1

Waza_Be
Waza_Be

Reputation: 39577

First idea:check the network operator, on the emulator, it's always equal to "Android". Not documented and just a guess that it will work everytime!

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

Second idea: the manufacturer:

public boolean isEmulator() {
    return Build.MANUFACTURER.equals("unknown");
}

Third idea: seem th best one, check the debug key:

static final String DEBUGKEY = 
      "get the debug key from logcat after calling the function below once from the emulator"; 




public static boolean signedWithDebugKey(Context context, Class<?> cls) 
{
    boolean result = false;
    try {
        ComponentName comp = new ComponentName(context, cls);
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(),PackageManager.GET_SIGNATURES);
        Signature sigs[] = pinfo.signatures;
        for ( int i = 0; i < sigs.length;i++)
        Log.d(TAG,sigs[i].toCharsString());
        if (DEBUGKEY.equals(sigs[0].toCharsString())) {
            result = true;
            Log.d(TAG,"package has been signed with the debug key");
        } else {
            Log.d(TAG,"package signed with a key other than the debug key");
        }

    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return false;
    }

    return result;

} 

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

Upvotes: 1

Dan S
Dan S

Reputation: 9189

Use the Build.DEVICE value and compare to "sdk".

Upvotes: 1

Related Questions