Android 获取手机信息相关的方法

因为这部分知识经常用到,因此整理一下,为了以后使用方便

1. 判断手机是否root

//判断手机是否root
public static boolean isRoot() {
    String binPath = "/system/bin/su";
    String xBinPath = "/system/xbin/su";

    if (new File(binPath).exists() && isCanExecute(binPath)) {
        return true;
    }
    if (new File(xBinPath).exists() && isCanExecute(xBinPath)) {
        return true;
    }
    return false;
}

private static boolean isCanExecute(String filePath) {
    java.lang.Process process = null;
    try {
        process = Runtime.getRuntime().exec("ls -l " + filePath);
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String str = in.readLine();
        if (str != null && str.length() >= 4) {
            char flag = str.charAt(3);
            if (flag == 's' || flag == 'x')
                return true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return false;
}

2. 获取MAC地址

/**
* 获取MAC地址
* 需添加权限<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
*/
public static String getMacAddress(Context context) {
    String macAddress;
    WifiManager wifi = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    macAddress = info.getMacAddress();
    if (null == macAddress) {
        return "";
    }
    macAddress = macAddress.replace(":", "");
    return macAddress;
}

3. 是否有SD卡

/**
* 是否有SD卡
*/
public static boolean haveSDCard() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

4. 判断设备是否是手机

/**
* 判断设备是否是手机
*/
public static boolean isPhone(Context context) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return telephony.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
}

5.获取当前设备的IMIE

/**
* 获取当前设备的IMIE,需与上面的isPhone一起使用
*/
public static String getDeviceIMEI(Context context) {
    String deviceId;
    if (isPhone(context)) {
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = telephony.getDeviceId();
    } else {
        deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
    return deviceId;
}

6. 判断手机是否为64位的方法

public static boolean is64bitDevice() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return Build.SUPPORTED_64_BIT_ABIS.length > 0;
    } else {
        return false;
    }
}

持续更新…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值