我希望我的代码在模拟器上运行时与在设备上运行时略有不同。(例如,使用10.0.2.2代替公共URL在开发服务器上自动运行。)检测Android应用程序何时在模拟器中运行的最佳方法是什么?
当前回答
上面建议的检查ANDROID_ID的解决方案对我来说很有效,直到我今天更新到Android 2.2发布的最新SDK工具。
因此,我目前切换到以下解决方案,到目前为止,缺点是你需要把PHONE_STATE读取权限(<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);
}
}
其他回答
安卓id不适合我,我现在用的是:
"google_sdk".equals( Build.PRODUCT );
由于Genymotion的底层模拟引擎是VirtualBox,这不会很快改变,我发现以下代码是最可靠的:
public static boolean isGenymotion() {
return Build.PRODUCT != null && Build.PRODUCT.contains("vbox");
}
我的建议:
试试github上的这个。
易于检测android模拟器
在设备场(https://aws.amazon.com/device-farm/)的真实设备上检查 BlueStacks Genymotion Android模拟器 安迪46.2.207.0 菜单玩 诺克斯应用播放器 Koplayer …
如何与示例一起使用:
EmulatorDetector.with(this)
.setCheckTelephony(true)
.addPackageName("com.bluestacks")
.setDebug(true)
.detect(new EmulatorDetector.OnEmulatorDetectorListener() {
@Override
public void onResult(boolean isEmulator) {
if(isEmulator){
// Do your work
}
else{
// Not emulator and do your work
}
}
});
这对我来说是有效的,而不是startwith: Build.FINGERPRINT.contains("generic")
欲了解更多信息,请查看这个链接:https://gist.github.com/espinchi/168abf054425893d86d1
最常用的方法是从品牌、名称……等。但是这个方法是静态的,并且适用于模拟器的有限版本。如果有1000多家虚拟机制造商呢?那么你必须写一段代码来匹配1000多个虚拟机?
但这是浪费时间。甚至在一段时间后,会有新的vm启动,你的脚本也会被浪费。
根据我的测试,我知道了 getRadioVersion()在虚拟机上返回空, 并返回版本号在真正的android设备。
public Boolean IsVM()
{
return android.os.Build.getRadioVersion().length() == 0;
}
//return true if VM
//return false if real
虽然有用,但我没有官方解释。
代码:http://github.com/Back-X/anti-vm/blob/main/android/anti-vm.b4a
发布:http://github.com/Back-X/anti-vm/releases/download/1/anti-vm.apk
推荐文章
- Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑
- 为什么构建类型与产品风味不同?
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标