开发中我们经常需要用到一些设备的信息,接下来直接给大家贴出一个常用的获取设备信息的工具类。
public class DeviceInfoUtils {
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
public static String getSystemLanguage() {
return Locale.getDefault().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*/
public static Locale[] getSystemLanguageList() {
return Locale.getAvailableLocales();
}
/**
* 获取当前手机系统版本号
*/
public static String getSystemVersion() {
return Build.VERSION.RELEASE;
}
/**
* 获取手机型号
*/
public static String getSystemModel() {
return Build.MODEL;
}
/**
* 获取设备宽度(px)
*/
public static int getDeviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取设备高度(px)
*/
public static int getDeviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
*/
@SuppressLint("MissingPermission")
public static String getIMEI(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 获取厂商名
*/
public static String getDeviceManufacturer() {
return Build.MANUFACTURER;
}
/**
* 获取产品名
*/
public static String getDeviceProduct() {
return Build.PRODUCT;
}
/**
* 获取手机品牌
*/
public static String getDeviceBrand() {
return Build.BRAND;
}
/**
* 获取手机型号
*/
public static String getDeviceModel() {
return Build.MODEL;
}
/**
* 获取手机主板名
*/
public static String getDeviceBoard() {
return Build.BOARD;
}
/**
* 设备名
*/
public static String getDeviceDevice() {
return Build.DEVICE;
}
/**
* 手机Fingerprint标识
* fingerprit 信息
*/
public static String getDeviceFubgerprint() {
return Build.FINGERPRINT;
}
/**
* 硬件名
*/
public static String getDeviceHardware() {
return Build.HARDWARE;
}
/**
* 主机
*/
public static String getDeviceHost() {
return Build.HOST;
}
/**
* 显示ID
*/
public static String getDeviceDisplay() {
return Build.DISPLAY;
}
/**
* ID
*/
public static String getDeviceId() {
return Build.ID;
}
/**
* 获取手机用户名
*/
public static String getDeviceUser() {
return Build.USER;
}
/**
* 获取手机 硬件序列号
*/
public static String getDeviceSerial() {
return Build.SERIAL;
}
/**
* 获取手机Android系统SDK
*/
public static int getDeviceSDK() {
return Build.VERSION.SDK_INT;
}
/**
* Codename
*/
public static String getDeviceCodename() {
return Build.VERSION.CODENAME;
}
/**
* Bootloader
*/
public static String getDeviceBootloader() {
return Build.BOOTLOADER;
}
/**
* 版本类型
*/
public static String getType() {
return Build.TYPE;
}
/**
* 安全patch 时间
*/
public static String getSecurityPatch() {
return Build.VERSION.SECURITY_PATCH;
}
/** 获取CPU架构
*
* @return
*/
public static String getFrameworkName() {
return Build.CPU_ABI;
}
public static String getCpuName() {
try {
FileReader fr = new FileReader("/proc/cpuinfo");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
String[] array = text.split(":\\s+", 2);
for (int i = 0; i < array.length; i++) {
}
return array[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**cpu核心数
*
* @return
*/
public static int getNumCores()
{
// Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter
{
@Override
public boolean accept(File pathname)
{
// Check if filename is "cpu", followed by a single digit number
if (Pattern.matches("cpu[0-9]", pathname.getName()))
{
return true;
}
return false;
}
}
try
{
// Get directory containing CPU info
File dir = new File("/sys/devices/system/cpu/");
// Filter to only list the devices we care about
File[] files = dir.listFiles(new CpuFilter());
// Return the number of cores (virtual CPU devices)
return files.length;
} catch (Exception e)
{
// Default to return 1 core
return 1;
}
}
/**获取当前CPU频率
*
* @return
*/
public static int getCurCPU(){
int result = 0;
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
br = new BufferedReader(fr);
String text = br.readLine();
result = Integer.parseInt(text.trim());
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (fr != null)
try{
fr.close();
}catch (IOException e){
e.printStackTrace();
}
if (br != null)
try{
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
return result;
}
/**
* 是否root
* @return
*/
public static boolean isSuEnable() {
File file = null;
String[] paths = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/", "/su/bin/"};
try {
for (String path : paths) {
file = new File(path + "su");
if (file.exists() && file.canExecute()) {
return true;
}
}
} catch (Exception x) {
x.printStackTrace();
}
return false;
}
/**
* 获取手机本地ip
* @return
*/
public static String getLocalIpAddress() {
StringBuilder sbIpAddress = new StringBuilder("");
try {
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
int one = 0;
int two = 0;
while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
one++;
Enumeration<InetAddress> enIp = ni.getInetAddresses();
while (enIp.hasMoreElements()) {
InetAddress inet = enIp.nextElement();
two++;
if (!inet.isLoopbackAddress()
&& (inet instanceof Inet4Address)) {
// return inet.getHostAddress().toString();
sbIpAddress.append( inet.getHostAddress().toString());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sbIpAddress.toString();
}
}
(里面获取IMEI号只在android10版本以下有用,10以上由于权限问题获取不到了)
会不定时更新安卓相关知识,喜欢的给点一下关注,感谢大家支持!!