android获取RAM、CPU频率、系统版本、CPU核数
参考链接:
https://www.jianshu.com/p/76d68d13c475
https://github.com/matthewYang92/Themis
获取CPU频率、核数的逻辑,是通过文件操作实现的,Android是基于Linux系统的,所以该方式针对不同的手机都是可靠的操作方式。
RAM:
public static final int DEVICEINFO_UNKNOWN = -1;/*** Calculates the total RAM of the device through Android API or /proc/meminfo.** @param c - Context object for current running activity.* @return Total RAM that the device has, or DEVICEINFO_UNKNOWN = -1 in the event of an error.*/@TargetApi(Build.VERSION_CODES.JELLY_BEAN)public static long getTotalMemory(Context c) {// 内存是按照1024计算int thundred = 1024;// memInfo.totalMem not supported in pre-Jelly Bean APIs.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);am.getMemoryInfo(memInfo);if (memInfo != null) {return memInfo.totalMem / (thundred * thundred);} else {return DEVICEINFO_UNKNOWN;}} else {// 低版本不做处理,因此默认返回0return DEVICEINFO_UNKNOWN;}}
CPU频率:
/*** 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.*/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[128];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]) && endIndex < buffer.length) {endIndex++;}String str = new String(buffer, 0, endIndex);// 频率是按照1000计算curFreq = Integer.parseInt(str) / 1000;arrayList.add(curFreq);} catch (NumberFormatException 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() {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() {@Overridepublic 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[1024];try {int length = stream.read(buffer);for (int i = 0; i < length; i++) {if (buffer[i] == '\n' || i == 0) {if (buffer[i] == '\n') i++;for (int j = i; j < length; j++) {int textIndex = j - i;//Text doesn't match query at some point.if (buffer[j] != 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[index] != '\n') {if (Character.isDigit(buffer[index])) {int start = index;index++;while (index < buffer.length && Character.isDigit(buffer[index])) {index++;}String str = new String(buffer, 0, start, index - start);return Integer.parseInt(str);}index++;}return DEVICEINFO_UNKNOWN;}
系统版本:
String systemVersion = android.os.Build.VERSION.RELEASE;
CPU核数:
/*** 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() {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;}
相关文章:
android获取RAM、CPU频率、系统版本、CPU核数
参考链接: https://www.jianshu.com/p/76d68d13c475 https://github.com/matthewYang92/Themis 获取CPU频率、核数的逻辑,是通过文件操作实现的,Android是基于Linux系统的,所以该方式针对不同的手机都是可靠的操作方式。 RAM&am…...
微信小程序python+nodejs+php+springboot+vue 讲座预约系统
讲座预约管理系统的用户是系统最根本使用者,按需要分析系统包括用户:学生、管理员。 管理员通过后台的登录页面,选择管理员权限后进行登录,管理员的权限包括学生信息管理和文章公告管理。讲座公告管理,添加讲座公告信息…...
嵌入式开发笔记:STM32的外设GPIO知识学习
GPIO简介: • GPIO ( General Purpose Input Output )通用输入输出口 • 可配置为 8 种输入输出模式 • 引脚电平: 0V~3.3V ,部分引脚可容忍 5V (如舵机和驱动直流电机) • 输出模式下可控制端口…...
单片机论文参考:2、基于单片机的病床呼叫系统设计
任务要求 设计病床呼叫系统,使用3X8矩阵开关分别模拟医院病房与病床位数,当某开关按下时,系统显示呼叫的病房与病床、呼叫的时间。处理完毕可清除该呼叫显示记录。同时有数个病床呼叫时,可以循环呼叫记录显示。 摘要 病房呼叫系统…...
【C语言】结构体实现位段!位段有何作用?
本篇文章目录 1. 声明位段2. 位段的内存分配3. 位段的跨平台问题4.位段的应用5. 如何解决位段的跨平台问题? 1. 声明位段 位段的声明和结构是类似的,有两个不同: 位段的成员必须是 int、unsigned int 或 char。位段的成员名后边有一个冒号和…...
msvcp140为什么会丢失?msvcp140.dll丢失的解决方法
msvcp140.dll 是一个动态链接库文件,它包含了 C 运行时库的一些函数和类,例如全局对象、异常处理、内存管理、文件操作等。它是 Visual Studio 2015 及以上版本中的一部分,用于支持 C 应用程序的运行。如果 msvcp140.dll 丢失或损坏ÿ…...
Ingress Controller
什么是 Ingress Controller ? 在云原生生态中,通常来讲,入口控制器( Ingress Controller )是 Kubernetes 中的一个关键组件,用于管理入口资源对象。 Ingress 资源对象用于定义来自外网的 HTTP 和 HTTPS 规则,以控制进…...
离线安装 K3S
一、前言 简要记录一下离线环境下 K3S 的搭建,版本为 v1.23.17k3s1,使用外部数据库 MySQL 作元数据存储,禁用默认组件(coredns、servicelb、traefik、local-storage、metrics-server)并使用 Helm 单独安装(…...
Error系列-常见异常问题解决方案以及系统指令总结
前情提要 作为一名开发,日常工作中会遇到很多报错的情况,希望我的总结可以帮助到小伙伴们~日常工作中也会遇到需要部署项目或者登陆linux系统操作的情况,很多时候需要查找一些命令,于是我决定,要把我日常经常用到的一…...
c 各种例子
1. struct{ int code; float cost; }item,*ptrst; ptrst&item; prtst->code3451 // ptrst->codeitem.code(*ptrst).code 结构与union 的运算符相同,不同的是union 在同一时间内只能存储成员中的一种,其他的成员不真实。 2. c的修饰符声…...
Flowable主要子流程介绍
1. 内嵌子流程 (1)说明 内嵌子流程又叫嵌入式子流程,它是一个可以包含其它活动、分支、事件,等的活动。我们通常意义上说的子流程通常就是指的内嵌子流程,它表现为将一个流程(子流程)定…...
通过插件去除Kotlin混淆去除 @Metadata标记
在Kotlin中,Metadata是指描述Kotlin类的元数据。它包含了关于类的属性、函数、注解和其他信息的描述。Metadata的作用主要有以下几个方面: 反射:Metadata可以用于在运行时获取类的信息,包括类的名称、属性、函数等。通过反射&…...
【docker】容器跟宿主机、其他容器通信
说明 容器跟宿主机、其他容器通信的关键在于它们要在同一个网络,或者通过修改路由信息来可以让它们互相之间能够找得到对方的 IP。本文主要介绍让它们在同一个网络的方法。 Docker 自定义网络模式介绍 Docker容器可以通过自定义网络来与宿主机或其他容器进行通信…...
nginx重要配置参数
1、https配置证书 nginx配置https访问_LMD菜鸟先飞的博客-CSDN博客 2、同一个端口代理多个页面 nginx同一个地址端口代理多个页面_同一ip,端口,访问不同页面 nginx_LMD菜鸟先飞的博客-CSDN博客 3、nginx访问压缩数据,加快访问速度 #gzip模块设置gzip on; #开启g…...
Docker 部署 PostgreSQL 服务
拉取最新版本的 PostgreSQL 镜像: $ sudo docker pull postgres:latest在本地预先创建好 data 目录, 用于映射 PostgreSQL 容器内的 /var/lib/postgresql/data 目录。 使用以下命令来运行 PostgreSQL 容器: $ sudo docker run -itd --name postgres -e POSTGRES_…...
【通信误码】python实现-附ChatGPT解析
1.题目 通信误码 时间限制: 1s 空间限制: 32MB 限定语言: 不限 题目描述: 信号传播过程中会出现一些误码,不同的数字表示不同的误码ID, 取值范围为1~65535,用一个数组“记录误码出现的情况。 每个误码出现的次数代表误码频度, 请找出记录中包含频度最高误码的最小子数组长度…...
人与机器只能感知到可以分类的事物?
众所周知,人与机器都能够感知和分类事物。人类拥有感官系统,如视觉、听觉、嗅觉、触觉和味觉,可以通过感知事物的外部特征和属性来进行分类。机器可以通过传感器和算法来感知和分类事物,比如计算机视觉技术可以通过图像和视频数据…...
2023华为杯数学建模竞赛E题
一、前言 颅内出血(ICH)是由多种原因引起的颅腔内出血性疾病,既包括自发性出血,又包括创伤导致的继发性出血,诊断与治疗涉及神经外科、神经内科、重症医学科、康复科等多个学科,是临床医师面临的重要挑战。…...
AIX360-CEMExplainer: MNIST Example
CEMExplainer: MNIST Example 这一部分屁话有点多,导包没问题的话可以跳过加载MNIST数据集加载经过训练的MNIST模型加载经过训练的卷积自动编码器模型(可选)初始化CEM解释程序以解释模型预测解释输入实例获得相关否定(Pertinent N…...
TouchGFX之自定义控件
在创建应用时,您可能需要TouchGFX中没有包含的控件。在创建应用时,您可能需要TouchGFX中没有包含的控件。但有时此法并不够用,当您需要全面控制帧缓冲时,您需要使用自定义控件法。 TouchGFX Designer目前不支持自定义控件的创建。…...
多模态2025:技术路线“神仙打架”,视频生成冲上云霄
文|魏琳华 编|王一粟 一场大会,聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中,汇集了学界、创业公司和大厂等三方的热门选手,关于多模态的集中讨论达到了前所未有的热度。其中,…...
智慧医疗能源事业线深度画像分析(上)
引言 医疗行业作为现代社会的关键基础设施,其能源消耗与环境影响正日益受到关注。随着全球"双碳"目标的推进和可持续发展理念的深入,智慧医疗能源事业线应运而生,致力于通过创新技术与管理方案,重构医疗领域的能源使用模式。这一事业线融合了能源管理、可持续发…...
Oracle查询表空间大小
1 查询数据库中所有的表空间以及表空间所占空间的大小 SELECTtablespace_name,sum( bytes ) / 1024 / 1024 FROMdba_data_files GROUP BYtablespace_name; 2 Oracle查询表空间大小及每个表所占空间的大小 SELECTtablespace_name,file_id,file_name,round( bytes / ( 1024 …...
理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端
🌟 什么是 MCP? 模型控制协议 (MCP) 是一种创新的协议,旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议,它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...
iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版分享
平时用 iPhone 的时候,难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵,或者买了二手 iPhone 却被原来的 iCloud 账号锁住,这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...
Golang dig框架与GraphQL的完美结合
将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用,可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器,能够帮助开发者更好地管理复杂的依赖关系,而 GraphQL 则是一种用于 API 的查询语言,能够提…...
什么是EULA和DPA
文章目录 EULA(End User License Agreement)DPA(Data Protection Agreement)一、定义与背景二、核心内容三、法律效力与责任四、实际应用与意义 EULA(End User License Agreement) 定义: EULA即…...
C++.OpenGL (10/64)基础光照(Basic Lighting)
基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...
IT供电系统绝缘监测及故障定位解决方案
随着新能源的快速发展,光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域,IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选,但在长期运行中,例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...
爬虫基础学习day2
# 爬虫设计领域 工商:企查查、天眼查短视频:抖音、快手、西瓜 ---> 飞瓜电商:京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空:抓取所有航空公司价格 ---> 去哪儿自媒体:采集自媒体数据进…...
