Android DeviceUtils-设备相关工具类

DeviceUtils是一个设备相关工具类

功能:

1.判断设备是否root

2.获取设备系统版本号

3.获取设备AndroidID

4.获取设备MAC地址

package com.blankj.utilcode.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;

import java.io.File;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

/**
 * 
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/1
 *     desc  : 设备相关工具类
 * 
*/ public class DeviceUtils { private DeviceUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 判断设备是否root * * @return the boolean{@code true}: 是
{@code false}: 否 */ public static boolean isDeviceRoot() { String su = "su"; String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/"}; for (String location : locations) { if (new File(location + su).exists()) { return true; } } return false; } /** * 获取设备系统版本号 * * @return 设备系统版本号 */ public static int getSDKVersion() { return android.os.Build.VERSION.SDK_INT; } /** * 获取设备AndroidID * * @param context 上下文 * @return AndroidID */ @SuppressLint("HardwareIds") public static String getAndroidID(Context context) { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } /** * 获取设备MAC地址 *

需添加权限 {@code }

*

需添加权限 {@code }

* * @param context 上下文 * @return MAC地址 */ public static String getMacAddress(Context context) { String macAddress = getMacAddressByWifiInfo(context); if (!"02:00:00:00:00:00".equals(macAddress)) { return macAddress; } macAddress = getMacAddressByNetworkInterface(); if (!"02:00:00:00:00:00".equals(macAddress)) { return macAddress; } macAddress = getMacAddressByFile(); if (!"02:00:00:00:00:00".equals(macAddress)) { return macAddress; } return "please open wifi"; }

你可能感兴趣的:(Android,公共类)