android获取硬件信息

£神魔★判官ぃ 2022-01-07 08:17 400阅读 0赞

1.获取CPU型号

  1. private static String getCpuName(){
  2. try{
  3. FileReader fr = new FileReader("/proc/cpuinfo");
  4. BufferedReader br = new BufferedReader(fr);
  5. String text = br.readLine();
  6. String[] array = text.split(":\\s+",2);
  7. for(int i = 0; i < array.length; i++){
  8. }
  9. return array[1];
  10. }catch(FileNotFoundExecption e){
  11. e.printStackTrace();
  12. }catch (IOException e){
  13. e.printStackTrace();
  14. }
  15. return null;
  16. }

2.获取CPU核心数

  1. private int getNumCores() {
  2. //Private Class to display only CPU devices in the directory listing
  3. class CpuFilter implements FileFilter {
  4. @Override
  5. public boolean accept(File pathname) {
  6. //Check if filename is "cpu", followed by a single digit number
  7. if(Pattern.matches("cpu[0-9]", pathname.getName())) {
  8. return true;
  9. }
  10. return false;
  11. }
  12. }
  13. try {
  14. //Get directory containing CPU info
  15. File dir = new File("/sys/devices/system/cpu/");
  16. //Filter to only list the devices we care about
  17. File[] files = dir.listFiles(new CpuFilter());
  18. Log.d(TAG, "CPU Count: "+files.length);
  19. //Return the number of cores (virtual CPU devices)
  20. return files.length;
  21. } catch(Exception e) {
  22. //Print exception
  23. Log.d(TAG, "CPU Count: Failed.");
  24. e.printStackTrace();
  25. //Default to return 1 core
  26. return 1;
  27. }
  28. }

3.获取CPU最大频率

  1. public static String getMinCpuFreq() {
  2. String result = "";
  3. ProcessBuilder cmd;
  4. try {
  5. String[] args = { "/system/bin/cat",
  6. "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
  7. cmd = new ProcessBuilder(args);
  8. Process process = cmd.start();
  9. InputStream in = process.getInputStream();
  10. byte[] re = new byte[24];
  11. while (in.read(re) != -1) {
  12. result = result + new String(re);
  13. }
  14. in.close();
  15. } catch (IOException ex) {
  16. ex.printStackTrace();
  17. result = "N/A";
  18. }
  19. return result.trim();
  20. }

4.RAM内存大小

  1. private long getRamMemory(Context context){
  2. String str1 = "/proc/meminfo";// 系统内存信息文件
  3. String str2;
  4. String[] arrayOfString;
  5. long initial_memory = 0;
  6. try
  7. {
  8. FileReader localFileReader = new FileReader(str1);
  9. BufferedReader localBufferedReader = new BufferedReader(
  10. localFileReader, 8192);
  11. str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
  12. arrayOfString = str2.split("\\s+");
  13. for (String num : arrayOfString) {
  14. Log.i(str2, num + "\t");
  15. }
  16. initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
  17. localBufferedReader.close();
  18. } catch (IOException e) {
  19. }
  20. //return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化
  21. System.out.println("总运存--->>>"+initial_memory/(1024*1024));
  22. return initial_memory/(1024*1024);
  23. }

5.ROM大小

  1. public long getTotalInternalMemorySize() {
  2. File path = Environment.getDataDirectory();
  3. StatFs stat = new StatFs(path.getPath());
  4. long blockSize = stat.getBlockSize();
  5. long totalBlocks = stat.getBlockCount();
  6. return totalBlocks * blockSize;
  7. }

6.获取屏幕分辨率

  1. private String getScreenResolution(){
  2. DisplayMetrics dm = new DisplayMetrics();
  3. getWindowManager().getDefaultDisplay().getMetrics(dm);
  4. String strOpt = dm.widthPixels + " * " + dm.heightPixels;
  5. return strOpt;
  6. }

转载于:https://www.cnblogs.com/chengliu/p/4130635.html

发表评论

表情:
评论列表 (有 0 条评论,400人围观)

还没有评论,来说两句吧...

相关阅读

    相关 技巧 获取电脑硬件信息 -转发

    概述 分享一个一键检测硬件命令。当你拿到一个新电脑时,不需要再去控制面板、设备管理器、电脑属性去一个一个查看了,只需要复制下这里脚本内容,一键执行就可以了解到硬件的所有信息