獲取螢幕亮度 記憶體 SD卡 cpu等屬性
阿新 • • 發佈:2018-12-21
剛之前的師傅讓我幫忙整理個工具類 ,整理好了,我也記錄下吧 ,說不定啥時間就要用到了
/** * 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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/su749520/article/details/79175392 **/ public static String getMemTotal() { String result = null; try { result = getFieldFromMeminfo("MemTotal"); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 獲取可用記憶體 * https://blog.csdn.net/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 ************************/ }