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

打印 上一主题 下一主题

主题 853|帖子 853|积分 2559

APK 获取设备信息

头文件
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileFilter;
  4. import java.io.FileInputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
复制代码
获取设备型号
  1. public static String getDeviceModel() {
  2.     return Build.MODEL;
  3. }
复制代码
获取设备制造商
  1. public static String getManufacturer() {
  2.     return Build.MANUFACTURER;
  3. }
复制代码
获取Android版本号
  1. public static String getAndroidVersion() {
  2.     return Build.VERSION.RELEASE;
  3. }
复制代码
获取SDK版本信息
  1. public static int getDeviceSDK(){
  2.     return android.os.Build.VERSION.SDK_INT;
  3. }
复制代码
获取当前设备安全补丁级别日期
  1. public static String getSecurityPatchLevel() {
  2.     return Build.VERSION.SECURITY_PATCH;
  3. }
复制代码
获取设备制造商
  1. public static String getDeviceMANUFACTURER() {
  2.     return Build.SOC_MANUFACTURER;
  3. }
复制代码
获取构建的内部版本

内部版本Build ID在rb-a3568/build/make/core/build_id.mk 目次下
  1. public static String getBuildID() {
  2.     return Build.ID;
  3. }
复制代码
获取显示信息
  1. public static String getDisplay() {
  2.     return Build.DISPLAY;
  3. }
复制代码
获取设备硬件名
  1. public static String getHardware() {
  2.     return Build.HARDWARE;
  3. }
复制代码
获取设备CPU架构
  1. public static String getCpuArchitecture() {
  2.     return Build.CPU_ABI;
  3. }
复制代码
获取CPU名称

通过读取Linux文件的情势获取
  1. public static String getCpuName() {
  2.     String str1 = "/proc/cpuinfo";
  3.     String str2 = "";
  4.     String cpuName = "";
  5.     try {
  6.         FileReader fileReader = new FileReader(str1);
  7.         BufferedReader bufferedReader = new BufferedReader(fileReader);
  8.         while ((str2 = bufferedReader.readLine()) != null) {
  9.             // 为空跳过
  10.             if (TextUtils.isEmpty(str2)) {
  11.                 continue;
  12.             }
  13.             // 进行 分割 对比
  14.             // 使用split(":\\s+", 2)方法将字符串str2按冒号和后续空格分割成最多两部分。例如,"Hardware: Intel Core i7"会被分成["Hardware", "Intel Core i7"]。
  15.             String[] arrayOfString = str2.split(":\\s+", 2);
  16.             if (TextUtils.equals(arrayOfString[0].trim(), "Hardware")) {
  17.                 cpuName = arrayOfString[1];
  18.                 break;
  19.             }
  20.         }
  21.         bufferedReader.close();
  22.         fileReader.close();
  23.     } catch (IOException e) {
  24.         e.printStackTrace();
  25.     }
  26.     return cpuName;
  27. }
复制代码
获取CPU核数
  1. public static String getCpuCores() {
  2.     return Build.CPU_ABI;
  3. }
复制代码
获取CPU频率
  1. public static final int DEVICEINFO_UNKNOWN = -1;
  2. /**
  3.      * Method for reading the clock speed of a CPU core on the device. Will read from either
  4.      * {@code /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq} or {@code /proc/cpuinfo}.
  5.      *
  6.      * @return Clock speed of a core on the device, or -1 in the event of an error.
  7.      * 获取 CPU 频率
  8.      */
  9. public static ArrayList<Integer> getCPUFreqMHzs() {
  10.     int maxFreq = DEVICEINFO_UNKNOWN;
  11.     int curFreq = 0;
  12.     ArrayList<Integer> arrayList = new ArrayList<Integer>();
  13.     try {
  14.         int coreNum = getNumberOfCPUCores();
  15.         for (int i = 0; i < coreNum; i++) {
  16.             String filename =
  17.                 "/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";
  18.             File cpuInfoMaxFreqFile = new File(filename);
  19.             if (cpuInfoMaxFreqFile.exists() && cpuInfoMaxFreqFile.canRead()) {
  20.                 byte[] buffer = new byte[128];
  21.                 FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
  22.                 try {
  23.                     stream.read(buffer);
  24.                     int endIndex = 0;
  25.                     // Trim the first number out of the byte buffer.
  26.                     while (Character.isDigit(buffer[endIndex]) && endIndex < buffer.length) {
  27.                         endIndex++;
  28.                     }
  29.                     String str = new String(buffer, 0, endIndex);
  30.                     // 频率是按照1000计算
  31.                     curFreq = Integer.parseInt(str) / 1000;
  32.                     arrayList.add(curFreq);
  33.                 } catch (NumberFormatException e) {
  34.                 } catch (IOException e) {
  35.                     throw new RuntimeException(e);
  36.                 } finally {
  37.                     stream.close();
  38.                 }
  39.             }
  40.         }
  41.         if (maxFreq == DEVICEINFO_UNKNOWN && arrayList.size() == 0) {
  42.             FileInputStream stream = new FileInputStream("/proc/cpuinfo");
  43.             try {
  44.                 int freqBound = parseFileForValue("cpu MHz", stream);
  45.                 curFreq = freqBound;
  46.                 arrayList.add(curFreq);
  47.             } finally {
  48.                 stream.close();
  49.             }
  50.         }
  51.     } catch (IOException e) {
  52.     }
  53.     return arrayList;
  54. }
  55. /**
  56.      * Reads the number of CPU cores from the first available information from
  57.      * {@code /sys/devices/system/cpu/possible}, {@code /sys/devices/system/cpu/present},
  58.      * then {@code /sys/devices/system/cpu/}.
  59.      *
  60.      * @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
  61.      */
  62. public static int getNumberOfCPUCores() {
  63.     int coreNumber = -1;
  64.     if (coreNumber != DEVICEINFO_UNKNOWN) {
  65.         return coreNumber;
  66.     }
  67.     if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
  68.         // Gingerbread doesn't support giving a single application access to both cores, but a
  69.         // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
  70.         // chipset and Gingerbread; that can let an app in the background run without impacting
  71.         // the foreground application. But for our purposes, it makes them single core.
  72.         coreNumber = 1;
  73.         return coreNumber;
  74.     }
  75.     int cores;
  76.     try {
  77.         cores = getCoresFromFileInfo("/sys/devices/system/cpu/present");
  78.         if (cores == DEVICEINFO_UNKNOWN) {
  79.             cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;;
  80.         }
  81.     } catch (SecurityException e) {
  82.         cores = DEVICEINFO_UNKNOWN;
  83.     } catch (NullPointerException e) {
  84.         cores = DEVICEINFO_UNKNOWN;
  85.     }
  86.     coreNumber = cores;
  87.     return coreNumber;
  88. }
  89. /**
  90.      * Tries to read file contents from the file location to determine the number of cores on device.
  91.      * @param fileLocation The location of the file with CPU information
  92.      * @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
  93.      */
  94. private static int getCoresFromFileInfo(String fileLocation) {
  95.     InputStream is = null;
  96.     try {
  97.         is = new FileInputStream(fileLocation);
  98.         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
  99.         String fileContents = buf.readLine();
  100.         buf.close();
  101.         return getCoresFromFileString(fileContents);
  102.     } catch (IOException e) {
  103.         return DEVICEINFO_UNKNOWN;
  104.     } finally {
  105.         if (is != null) {
  106.             try {
  107.                 is.close();
  108.             } catch (IOException e) {
  109.                 // Do nothing.
  110.             }
  111.         }
  112.     }
  113. }
  114. /**
  115.      * Converts from a CPU core information format to number of cores.
  116.      * @param str The CPU core information string, in the format of "0-N"
  117.      * @return The number of cores represented by this string
  118.      */
  119. private static int getCoresFromFileString(String str) {
  120.     if (str == null || !str.matches("0-[\\d]+$")) {
  121.         return DEVICEINFO_UNKNOWN;
  122.     }
  123.     return Integer.valueOf(str.substring(2)) + 1;
  124. }
  125. private static final FileFilter CPU_FILTER = new FileFilter() {
  126.     @Override
  127.     public boolean accept(File pathname) {
  128.         String path = pathname.getName();
  129.         //regex is slow, so checking char by char.
  130.         if (path.startsWith("cpu")) {
  131.             for (int i = 3; i < path.length(); i++) {
  132.                 if (!Character.isDigit(path.charAt(i))) {
  133.                     return false;
  134.                 }
  135.             }
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140. };
  141. /**
  142.      * Helper method for reading values from system files, using a minimised buffer.
  143.      *
  144.      * @param textToMatch - Text in the system files to read for.
  145.      * @param stream      - FileInputStream of the system file being read from.
  146.      * @return A numerical value following textToMatch in specified the system file.
  147.      * -1 in the event of a failure.
  148.      */
  149. private static int parseFileForValue(String textToMatch, FileInputStream stream) {
  150.     byte[] buffer = new byte[1024];
  151.     try {
  152.         int length = stream.read(buffer);
  153.         for (int i = 0; i < length; i++) {
  154.             if (buffer[i] == '\n' || i == 0) {
  155.                 if (buffer[i] == '\n') i++;
  156.                 for (int j = i; j < length; j++) {
  157.                     int textIndex = j - i;
  158.                     //Text doesn't match query at some point.
  159.                     if (buffer[j] != textToMatch.charAt(textIndex)) {
  160.                         break;
  161.                     }
  162.                     //Text matches query here.
  163.                     if (textIndex == textToMatch.length() - 1) {
  164.                         return extractValue(buffer, j);
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.     } catch (IOException e) {
  170.         //Ignore any exceptions and fall through to return unknown value.
  171.     } catch (NumberFormatException e) {
  172.     }
  173.     return DEVICEINFO_UNKNOWN;
  174. }
  175. /**
  176.      * Helper method used by {@link #parseFileForValue(String, FileInputStream) parseFileForValue}. Parses
  177.      * the next available number after the match in the file being read and returns it as an integer.
  178.      * @param index - The index in the buffer array to begin looking.
  179.      * @return The next number on that line in the buffer, returned as an int. Returns
  180.      * DEVICEINFO_UNKNOWN = -1 in the event that no more numbers exist on the same line.
  181.      */
  182. private static int extractValue(byte[] buffer, int index) {
  183.     while (index < buffer.length && buffer[index] != '\n') {
  184.         if (Character.isDigit(buffer[index])) {
  185.             int start = index;
  186.             index++;
  187.             while (index < buffer.length && Character.isDigit(buffer[index])) {
  188.                 index++;
  189.             }
  190.             String str = new String(buffer, 0, start, index - start);
  191.             return Integer.parseInt(str);
  192.         }
  193.         index++;
  194.     }
  195.     return DEVICEINFO_UNKNOWN;
  196. }
复制代码
设备信息源码位置修改

build\make\core\sysprop.mk




  • 搜刮到数据很多,根据当前使用的Rockchip 3568的芯片,找到 3568对应的文件夹下的 device/rockchip/rk356x/rk3568_t/rk3568_t.mk



  • 根据需求修改相应的参数

参考资料

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

何小豆儿在此

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表