【问题标题】:android is it possible for an app to detect when it is running on emulator vs deviceandroid 应用程序是否可以检测它何时在模拟器和设备上运行
【发布时间】:2011-09-12 08:08:11
【问题描述】:
我开发了一个严重依赖时间的游戏,当我在模拟器中运行它时,它的执行速度比我在手机上运行时慢,这是可以理解的。这迫使我提高游戏中的所有“统计数据”,以便我在开发时实际上可以“击败它”——在调试时,游戏无法获胜。
是否有调用、变量或其他东西可用于确定我当前是否在模拟器而不是设备上运行。
我考虑过尝试检测Framerate 是否低。
我考虑过尝试从某种内置系统字段中读取“设备名称”。
但这两种方法似乎都不是一个很好的追求方法。
任何帮助都会很棒。
【问题讨论】:
标签:
android
android-emulator
【解决方案2】:
第一个想法:检查网络运营商,在模拟器上,它总是等于“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?
【解决方案3】:
如果您使用的是 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
}
【解决方案4】:
Dan S给出了一个很好的答案,关于如何在模拟器上运行时检测。但是,一些提示:
- 与其依赖 SDK 中的某些东西,不如设置自己的标志?只需保留一个
public final static boolean isEmulator,根据环境从true 更改为false,并在其周围使用ifs 和elses 构建您的代码。 Build.DEVICE 方法不是 100% 安全的,因为某些 root 设备可能会出现这种情况。
- 低帧率检测可能是一件好事。鉴于 Android 设备种类繁多,它可能对低端设备有所帮助。