Android 判断真机和模拟器的方法
Android判断真机和模拟器的方法
最近有一些业务需求要判断是否在真机上运行还是在模拟器上运行两种不同的情况下做不同的业务逻辑操作。上网查了查还真有不少的资源。
接下来给大家展示下实例代码:
privatestaticStringgetSystemProperty(Stringname)throwsException{
ClasssystemPropertyClazz=Class.forName("android.os.SystemProperties");
return(String)systemPropertyClazz.getMethod("get",newClass[]{String.class})
.invoke(systemPropertyClazz,newObject[]{name});
}
publicstaticbooleancheckEmulator(){
try{
booleangoldfish=getSystemProperty("ro.hardware").contains("goldfish");
booleanemu=getSystemProperty("ro.kernel.qemu").length()>0;
booleansdk=getSystemProperty("ro.product.model").equals("sdk");
if(emu||goldfish||sdk){
returntrue;
}
}catch(Exceptione){
}
returnfalse;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!