您的位置:首页 > 移动开发 > Android开发

android检测运行设备(模拟器or真机)

2016-12-02 10:41 197 查看
模拟器和真实的设备存在许多差异,我们可以使用命令“adb shell getprop”查看属性值,并做对比。经过对比发现,有如下几个属性值可以用来判断软件是否运行在模拟器中:

ro.product.model:该值在模拟器中为sdk,通常在正常手机中它的值为手机的型号。

ro.build.tags:该值在模拟器中为test-keys,通常在正常手机中它的值为release-keys。

ro.kernel.qemu:该值在模拟器中为1,通常在正常手机中没有该属性。

下面以检查ro.kernel.qemu属性为例,代码如下:

try {
process = Runtime.getRuntime().exec("getprop ro.kernel.qemu");
os = new DataOutputStream(process.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"gbk"));
os.writeBytes("exit\n");
os.flush();
process.waitFor();
qemuKernel = (Integer.valueOf(in.readLine()) == 1);
} catch (IOException e) {
qemuKernel = false;
e.printStackTrace();
} catch (InterruptedException e) {
qemuKernel = false;
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
process.destroy();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 手机 shell