模拟器和真机检测

以前在判断当前设备是真机还是模拟器,我们一般用检测当前设备是否含有光线传感器来判断,一般模拟器检测不到光线传感器

旧代码
/**
 * 方法:判断当前设备是模拟器还是真机
 * @param context
 * @return true 模拟器,false 真机
 */
public static Boolean isAndroidSimulator(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
    Sensor        sensor8       = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光
    if (null == sensor8) {
        return true;
    } else {
        return false;
    }
}

后来一些模拟器的新版本,也能检测到光线传感器,所以以上方法就不太实用了
所以我们加入判断当前设备是否能够拨打电话来尽情补充判断

新代码
/**
 * 方法:判断当前设备是模拟器还是真机
 * @param context
 * @return true 模拟器,false 真机
 */
public static Boolean isAndroidSimulator(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
    //是否含有光传感器
    Sensor        sensor8       = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光
    String        url           = "tel:" + "123456";
    Intent        intent        = new Intent();
    intent.setData(Uri.parse(url));
    intent.setAction(Intent.ACTION_DIAL);
    //是否能够不打电话
    boolean canCallPhone = intent.resolveActivity(context.getPackageManager()) != null;
    Logs.e("canCallPhone = " + canCallPhone);
    if (null == sensor8 || !canCallPhone) {
        return true;
    } else {
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值