开发者

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

I develop a game that is heavily d开发者_Go百科ependent 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.


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


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?


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
  }  


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜