Android 判断真机与模拟器

Android 判断真机与模拟器

package com.cordova.extraInfo.utils;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static android.content.Context.SENSOR_SERVICE;

public class SimulatorUtil {

    public static boolean getReslut(Context context) {
        try {
            String url = "tel:" + "123456";
            Intent intent = new Intent();
            intent.setData(Uri.parse(url));
            intent.setAction(Intent.ACTION_DIAL);
            // 是否可以处理跳转到拨号的 Intent
            boolean canCallPhone = intent.resolveActivity(context.getPackageManager()) != null;
            boolean cpuFlag = getCpuInfo();
            if (canCallPhone && JudgePhone(context) && cpuFlag) {
                Log.d("asdf", "getReslut" + true);
                return true;
            } else {
                Log.d("asdf", "getReslut" + false);
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 判断是否是真机
     * 通过手机光传感器来判断
     * 不确定全部机型判断准确
     */
    public static boolean JudgePhone(Context mContext) {
        try {
            SensorManager sensorManager = (SensorManager) mContext.getSystemService(SENSOR_SERVICE);
            Sensor sensor8 = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光
            if (null == sensor8) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 根据CPU是否为电脑来判断是否为模拟器(子方法)
     * 返回:false  模拟器
     * true  真机
     */
    public static boolean getCpuInfo() {
        String result = "";
        boolean flag = true;
        try {
            String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            StringBuffer sb = new StringBuffer();
            String readLine = "";
            BufferedReader responseReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));
            while ((readLine = responseReader.readLine()) != null) {
                sb.append(readLine);
                if (!TextUtils.isEmpty(readLine)) {
                    if (readLine.contains("Hardware")) {
                        if (readLine.contains("placeholder")) {
                            flag = false;
                        }
                    } else if (readLine.contains("Revision")) {
                        if (readLine.contains("000b")) {
                            flag = false;
                        }
                    }
                }
            }
            responseReader.close();
            result = sb.toString().toLowerCase();
        } catch (IOException ex) {

        }
        return flag;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值