获取设备信息的工具类

来源:互联网 发布:mac 开机 问号 编辑:程序博客网 时间:2024/06/06 06:47
/** * 描述:获取设备的硬件信息 * 创建人:菜籽 * 创建时间:2017/1/5 0005 19:33 * 备注: */public class DeviceInformation {    private static Properties properties;    /**     * 获取当前设备的IMEI信息     *     * @param context 上下文     * @param slotID  卡槽ID(0或者1)     * @return     */    public static String getDeviceIMEI(Context context, int slotID) {        String deviceIMEI = getOperatorBySlot(context, "getDeviceId", slotID);        return deviceIMEI;    }    /**     * 获取双卡手机的IMEI     *     * @param context     * @param predictedMethodName     * @param slotID     * @return     */    public static String getOperatorBySlot(Context context, String predictedMethodName, int slotID) {        String inumeric = null;        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);        try {            Class<?> telephonyClass = Class.forName(telephony.getClass().getName());            Class<?>[] parameter = new Class[1];            parameter[0] = int.class;            Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);            Object[] obParameter = new Object[1];            obParameter[0] = slotID;            Object ob_phone = getSimID.invoke(telephony, obParameter);            if (ob_phone != null) {                inumeric = ob_phone.toString();            }        } catch (Exception e) {            e.printStackTrace();        }        return inumeric;    }    /**     * 获取MAC地址,安卓7.0以上设备也可使用     *     * @param mContext     * @return     */    public static String getMacAddress(Context mContext) {        String macStr = "";        WifiManager wifiManager = (WifiManager) mContext                .getSystemService(Context.WIFI_SERVICE);        WifiInfo wifiInfo = wifiManager.getConnectionInfo();        if (wifiInfo.getMacAddress() != null) {            macStr = wifiInfo.getMacAddress();// MAC地址        } else {            macStr = "null";        }        return macStr;    }    /**     * 获取设备网卡MAC地址,仅限安卓7.0以下设备     *     * @return     */    public static String getDeviceMac(Activity context) {        String macSerial = "";        try {            Process pp = Runtime.getRuntime().exec(                    "cat /sys/class/net/wlan0/address");            InputStreamReader ir = new InputStreamReader(pp.getInputStream());            LineNumberReader input = new LineNumberReader(ir);            String line;            while ((line = input.readLine()) != null) {                macSerial += line.trim();            }            input.close();        } catch (IOException e) {            e.printStackTrace();        }        return macSerial;    }    /**     * 获取设备厂商     *     * @return     */    public static String getDeviceFactory() {        return android.os.Build.BRAND;    }    /**     * 获取手机型号     *     * @return     */    public static String getDeviceType() {        return android.os.Build.MODEL;    }    /**     * 获取当前Android版本     *     * @return     */    public static String getDeviceAndroidVersion() {        return android.os.Build.VERSION.RELEASE;    }    /**     * 获取当前ROM类型是否为MIUI     *     * @return     */    public static String getDeviceROMType() {//        return "未知";        String version = properties.getProperty("ro.miui.ui.version.name");        if (version != null) {            return "MIUI";        } else {            return "未知";        }    }    /**     * 获取当前设备的MIUI版本     */    public static String getDeviceMIUIVersion() {        return getProperties().getProperty("ro.build.version.incremental");    }    /**     * 判断当前版本:体验版/开发版/稳定版     *     * @return     */    public static String getCurrentVersion() {        String version = null;        String stringAlpha = properties.getProperty("ro.product.mod_device");        String stringVersion = properties.getProperty("ro.build.version.incremental");        if (stringAlpha != null) {            version = "alpha";        } else if (stringVersion.matches("\\d+.\\d+.\\d+(-internal)?")) {            version = "dev";        } else {            version = "stable";        }        return version;    }    /**     * 判断当前机型是否是定制机,用于小米手机     *     * @return     */    public static String getCustomize() {        String customize = getProperties().getProperty("ro.carrier.name");        if (customize != null) {            return customize;        }        return null;    }    /**     * 获取Properties     *     * @return     */    public static Properties getProperties() {        if (properties == null) {            synchronized (DeviceInformation.class) {                if (properties == null) {                    try {                        File propFile = new File(Environment.getRootDirectory(), "build.prop");                        FileInputStream inputStream = null;                        inputStream = new FileInputStream(propFile);                        properties = new Properties();                        properties.load(inputStream);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }        }        return properties;    }    /**     * 判断当前是否单卡机型,如果status=TelephonyManager.SIM_STATE_UNKNOWN则为单卡机型,否则为双卡     *     * @param context     * @return     */    public static boolean isDoubleSimPhone(Context context) {        TelephonyManager mTelephonyManager = (TelephonyManager) context.                getSystemService(Context.TELEPHONY_SERVICE);        Class<TelephonyManager> clz = (Class<TelephonyManager>) mTelephonyManager.getClass();        int status = 0;        try {            Method mtd = clz.getMethod("getSimState", int.class);            mtd.setAccessible(true);            status = (Integer) mtd.invoke(mTelephonyManager, 1);        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (IllegalArgumentException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }        return status == TelephonyManager.SIM_STATE_UNKNOWN;    }}
原创粉丝点击