何小豆儿在此 发表于 2025-2-13 19:37:36

Android设备基础信息获取 源码修改方式 APK开辟

APK 获取设备信息

头文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;获取设备型号

public static String getDeviceModel() {
    return Build.MODEL;
}获取设备制造商

public static String getManufacturer() {
    return Build.MANUFACTURER;
}获取Android版本号

public static String getAndroidVersion() {
    return Build.VERSION.RELEASE;
}获取SDK版本信息

public static int getDeviceSDK(){
    return android.os.Build.VERSION.SDK_INT;
}获取当前设备安全补丁级别日期

public static String getSecurityPatchLevel() {
    return Build.VERSION.SECURITY_PATCH;
}获取设备制造商

public static String getDeviceMANUFACTURER() {
    return Build.SOC_MANUFACTURER;
}获取构建的内部版本

内部版本Build ID在rb-a3568/build/make/core/build_id.mk 目次下
public static String getBuildID() {
    return Build.ID;
}获取显示信息

public static String getDisplay() {
    return Build.DISPLAY;
}获取设备硬件名

public static String getHardware() {
    return Build.HARDWARE;
}获取设备CPU架构

public static String getCpuArchitecture() {
    return Build.CPU_ABI;
}获取CPU名称

通过读取Linux文件的情势获取
public static String getCpuName() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String cpuName = "";
    try {
      FileReader fileReader = new FileReader(str1);
      BufferedReader bufferedReader = new BufferedReader(fileReader);
      while ((str2 = bufferedReader.readLine()) != null) {
            // 为空跳过
            if (TextUtils.isEmpty(str2)) {
                continue;
            }
            // 进行 分割 对比
            // 使用split(":\\s+", 2)方法将字符串str2按冒号和后续空格分割成最多两部分。例如,"Hardware: Intel Core i7"会被分成["Hardware", "Intel Core i7"]。
            String[] arrayOfString = str2.split(":\\s+", 2);
            if (TextUtils.equals(arrayOfString.trim(), "Hardware")) {
                cpuName = arrayOfString;
                break;
            }
      }
      bufferedReader.close();
      fileReader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return cpuName;
}获取CPU核数

public static String getCpuCores() {
    return Build.CPU_ABI;
}获取CPU频率

public static final int DEVICEINFO_UNKNOWN = -1;
/**
   * Method for reading the clock speed of a CPU core on the device. Will read from either
   * {@code /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq} or {@code /proc/cpuinfo}.
   *
   * @return Clock speed of a core on the device, or -1 in the event of an error.
   * 获取 CPU 频率
   */
public static ArrayList<Integer> getCPUFreqMHzs() {
    int maxFreq = DEVICEINFO_UNKNOWN;
    int curFreq = 0;
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    try {
      int coreNum = getNumberOfCPUCores();
      for (int i = 0; i < coreNum; i++) {
            String filename =
                "/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";
            File cpuInfoMaxFreqFile = new File(filename);
            if (cpuInfoMaxFreqFile.exists() && cpuInfoMaxFreqFile.canRead()) {
                byte[] buffer = new byte;
                FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
                try {
                  stream.read(buffer);
                  int endIndex = 0;
                  // Trim the first number out of the byte buffer.
                  while (Character.isDigit(buffer) && endIndex < buffer.length) {
                        endIndex++;
                  }
                  String str = new String(buffer, 0, endIndex);
                  // 频率是按照1000计算
                  curFreq = Integer.parseInt(str) / 1000;
                  arrayList.add(curFreq);
                } catch (NumberFormatException e) {
                } catch (IOException e) {
                  throw new RuntimeException(e);
                } finally {
                  stream.close();
                }
            }
      }
      if (maxFreq == DEVICEINFO_UNKNOWN && arrayList.size() == 0) {
            FileInputStream stream = new FileInputStream("/proc/cpuinfo");
            try {
                int freqBound = parseFileForValue("cpu MHz", stream);
                curFreq = freqBound;
                arrayList.add(curFreq);
            } finally {
                stream.close();
            }
      }
    } catch (IOException e) {
    }
    return arrayList;
}
/**
   * Reads the number of CPU cores from the first available information from
   * {@code /sys/devices/system/cpu/possible}, {@code /sys/devices/system/cpu/present},
   * then {@code /sys/devices/system/cpu/}.
   *
   * @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
   */
public static int getNumberOfCPUCores() {
    int coreNumber = -1;
    if (coreNumber != DEVICEINFO_UNKNOWN) {
      return coreNumber;
    }
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
      // Gingerbread doesn't support giving a single application access to both cores, but a
      // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
      // chipset and Gingerbread; that can let an app in the background run without impacting
      // the foreground application. But for our purposes, it makes them single core.
      coreNumber = 1;
      return coreNumber;
    }
    int cores;
    try {
      cores = getCoresFromFileInfo("/sys/devices/system/cpu/present");
      if (cores == DEVICEINFO_UNKNOWN) {
            cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;;
      }
    } catch (SecurityException e) {
      cores = DEVICEINFO_UNKNOWN;
    } catch (NullPointerException e) {
      cores = DEVICEINFO_UNKNOWN;
    }
    coreNumber = cores;
    return coreNumber;
}

/**
   * Tries to read file contents from the file location to determine the number of cores on device.
   * @param fileLocation The location of the file with CPU information
   * @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
   */
private static int getCoresFromFileInfo(String fileLocation) {
    InputStream is = null;
    try {
      is = new FileInputStream(fileLocation);
      BufferedReader buf = new BufferedReader(new InputStreamReader(is));
      String fileContents = buf.readLine();
      buf.close();
      return getCoresFromFileString(fileContents);
    } catch (IOException e) {
      return DEVICEINFO_UNKNOWN;
    } finally {
      if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // Do nothing.
            }
      }
    }
}

/**
   * Converts from a CPU core information format to number of cores.
   * @param str The CPU core information string, in the format of "0-N"
   * @return The number of cores represented by this string
   */
private static int getCoresFromFileString(String str) {
    if (str == null || !str.matches("0-[\\d]+$")) {
      return DEVICEINFO_UNKNOWN;
    }
    return Integer.valueOf(str.substring(2)) + 1;
}

private static final FileFilter CPU_FILTER = new FileFilter() {
    @Override
    public boolean accept(File pathname) {
      String path = pathname.getName();
      //regex is slow, so checking char by char.
      if (path.startsWith("cpu")) {
            for (int i = 3; i < path.length(); i++) {
                if (!Character.isDigit(path.charAt(i))) {
                  return false;
                }
            }
            return true;
      }
      return false;
    }
};

/**
   * Helper method for reading values from system files, using a minimised buffer.
   *
   * @param textToMatch - Text in the system files to read for.
   * @param stream      - FileInputStream of the system file being read from.
   * @return A numerical value following textToMatch in specified the system file.
   * -1 in the event of a failure.
   */
private static int parseFileForValue(String textToMatch, FileInputStream stream) {
    byte[] buffer = new byte;
    try {
      int length = stream.read(buffer);
      for (int i = 0; i < length; i++) {
            if (buffer == '\n' || i == 0) {
                if (buffer == '\n') i++;
                for (int j = i; j < length; j++) {
                  int textIndex = j - i;
                  //Text doesn't match query at some point.
                  if (buffer != textToMatch.charAt(textIndex)) {
                        break;
                  }
                  //Text matches query here.
                  if (textIndex == textToMatch.length() - 1) {
                        return extractValue(buffer, j);
                  }
                }
            }
      }
    } catch (IOException e) {
      //Ignore any exceptions and fall through to return unknown value.
    } catch (NumberFormatException e) {
    }
    return DEVICEINFO_UNKNOWN;
}

/**
   * Helper method used by {@link #parseFileForValue(String, FileInputStream) parseFileForValue}. Parses
   * the next available number after the match in the file being read and returns it as an integer.
   * @param index - The index in the buffer array to begin looking.
   * @return The next number on that line in the buffer, returned as an int. Returns
   * DEVICEINFO_UNKNOWN = -1 in the event that no more numbers exist on the same line.
   */
private static int extractValue(byte[] buffer, int index) {
    while (index < buffer.length && buffer != '\n') {
      if (Character.isDigit(buffer)) {
            int start = index;
            index++;
            while (index < buffer.length && Character.isDigit(buffer)) {
                index++;
            }
            String str = new String(buffer, 0, start, index - start);
            return Integer.parseInt(str);
      }
      index++;
    }
    return DEVICEINFO_UNKNOWN;
}设备信息源码位置修改

build\make\core\sysprop.mk
https://img2024.cnblogs.com/blog/3153674/202502/3153674-20250213201634780-625697027.png
https://img2024.cnblogs.com/blog/3153674/202502/3153674-20250213201714519-445812085.png


[*]搜刮到数据很多,根据当前使用的Rockchip 3568的芯片,找到 3568对应的文件夹下的 device/rockchip/rk356x/rk3568_t/rk3568_t.mk
https://img2024.cnblogs.com/blog/3153674/202502/3153674-20250213201729542-236633477.png


[*]根据需求修改相应的参数
https://img2024.cnblogs.com/blog/3153674/202502/3153674-20250213201747739-1580677205.png
参考资料

android利用StorageStatsManager获取应用程序的存储信息

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Android设备基础信息获取 源码修改方式 APK开辟