Android DeviceUtils-裝置相關工具類
阿新 • • 發佈:2019-01-31
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; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2016/8/1 * desc : 裝置相關工具類 * </pre> */ public class DeviceUtils { private DeviceUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 判斷裝置是否root * * @return the boolean{@code true}: 是<br>{@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地址 * <p>需新增許可權 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p> * <p>需新增許可權 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p> * * @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"; }