刚之前的师傅让我帮忙整理个工具类 ,整理好了,我也记录下吧 ,说不定啥时间就要用到了
/** * Description:获取手机硬件的工具类 * Author: ydd * Date:2018/11/16 */ public class PhoneUtils { private final static String CurPath = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq";//保存当前CPU频率 private final static String MaxPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";//保存CPU可运行最大频率 private final static String MinPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq";//保存CPU可运行最小频率 /** * 获取屏幕亮度 * https://blog.youkuaiyun.com/silence_cdsn/article/details/8185766 * 不同手机的屏幕亮度 最大不同 ,最大的是255,可是我这个手机最大只能调到165,最小调到4 **/ public static int getScreenBrightness(Activity activity) { int value = 0; ContentResolver cr = activity.getContentResolver(); try { value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS); } catch (Settings.SettingNotFoundException e) { } return value; } /** * 获取SD卡总量、SD卡已使用量 * https://blog.youkuaiyun.com/watermusicyes/article/details/37883883 */ public static String getMemoryInfo(Context context) { // 获得一个磁盘状态对象 StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); long blockSize = stat.getBlockSize(); // 获得一个扇区的大小 long totalBlocks = stat.getBlockCount(); // 获得扇区的总数 long availableBlocks = stat.getAvailableBlocks(); // 获得可用的扇区数量 // 总空间 String totalMemory = Formatter.formatFileSize(context, totalBlocks * blockSize); // 可用空间 String availableMemory = Formatter.formatFileSize(context, availableBlocks * blockSize); return "总空间: " + totalMemory + "\n可用空间: " + availableMemory; } /************************ START CPU ************************/ /** * 获取当前CPU频率 * https://blog.youkuaiyun.com/Csdn_Ting377/article/details/53447811 **/ public static int getCurCPU() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(CurPath); 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; } /** * 获取CPU可运行最大频率 * https://blog.youkuaiyun.com/Csdn_Ting377/article/details/53447811 **/ public static int getMaxCPU() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(MaxPath); 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; } /** * 获取CPU可运行最小频率 * https://blog.youkuaiyun.com/Csdn_Ting377/article/details/53447811 **/ public static int getMinCPU() { int result = 0; FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(MinPath); 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; } /************************ END CPU ************************/ /************************ START MEMORY ************************/ /** * 获取总内存 * https://blog.youkuaiyun.com/su749520/article/details/79175392 **/ public static String getMemTotal() { String result = null; try { result = getFieldFromMeminfo("MemTotal"); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 获取可用内存 * https://blog.youkuaiyun.com/su749520/article/details/79175392 **/ public static String getMemAvailable() { String result = null; try { result = getFieldFromMeminfo("MemAvailable"); } catch (IOException e) { e.printStackTrace(); } return result; } private static String getFieldFromMeminfo(String field) throws IOException { BufferedReader br = new BufferedReader(new FileReader("/proc/meminfo")); Pattern p = Pattern.compile(field + "\\s*:\\s*(.*)"); try { String line; while ((line = br.readLine()) != null) { Matcher m = p.matcher(line); if (m.matches()) { return m.group(1); } } } finally { br.close(); } return null; } /************************ END MEMORY ************************/ }