android是否可以让应用程序检测它何时在模拟器与设备上运行

时间:2011-09-12 08:08:12

标签: android android-emulator

我开发的游戏严重依赖于时间,当我在模拟器中运行时,它比我在手机上运行速度慢得多。这迫使我提升游戏中的所有“统计数据”,以便在开发时我能够“击败它” - 当调试时,游戏无法取胜。

是否有调用,变量或其他内容可用于确定我当前是在模拟器上运行而不是在设备上运行。

我考虑过尝试检测Framerate是否为低。 我曾考虑尝试从系统领域的某种构建中读取“设备名称”。

但这似乎都不是一个很好的方法。

任何帮助都会很棒。

4 个答案:

答案 0 :(得分:1)

使用Build.DEVICE值并与“sdk”进行比较。

答案 1 :(得分:1)

第一个想法:检查网络运营商,在模拟器上,它总是等于“Android”。没有记录,只是猜测它每次都会起作用!

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

第二个想法:制造商:

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

第三个想法:看起来最好的一个,检查调试密钥:

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;

} 

从这里开始:How can I detect when an Android application is running in the emulator?

答案 2 :(得分:1)

如果您使用的是Google API,则需要:

"google_sdk".equals( Build.PRODUCT );

如果没有,你会想要使用:

"sdk".equals( Build.PRODUCT );

较早(因为已弃用)的方法是检查ANDROID_ID,它在AVD上为空,但这不适用于API 7及更高版本:

// ONLY WORKS ON 2.1 AND BELOW

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

答案 3 :(得分:0)

Dan S 就如何检测何时在模拟器上运行提供了一个很好的答案。但是,一些提示:

  1. 为什么不设置自己的旗帜,而不是依赖SDK中的内容?只需根据环境保持public final static boolean isEmulatortrue更改为false,并使用ifs和elses构建代码。 Build.DEVICE方法不是100%安全的,因为某些有根设备的设备可能会被塞进去。
  2. 低帧率检测可能是一件好事。鉴于Android设备种类繁多,它可能会对低端产品有所帮助。
相关问题