Android开发之获取手机硬件状态信息(CPU信息/频率/使用率、DDR频率/使用率、电池瞬时电流/电压/库伦counter)

您所在的位置:网站首页 手机查看cpu频率对应的电压 Android开发之获取手机硬件状态信息(CPU信息/频率/使用率、DDR频率/使用率、电池瞬时电流/电压/库伦counter)

Android开发之获取手机硬件状态信息(CPU信息/频率/使用率、DDR频率/使用率、电池瞬时电流/电压/库伦counter)

2024-07-10 18:46| 来源: 网络整理| 查看: 265

有时候我们想要知道当前手机的一些状态信息,可以使用app(root 或者系统签名 )来显示获取。

OK,接下来看一下一些关键的代码。

我这里使用的是高通的手机,不同硬件平台的机型,其获取信息的节点可能不一样。

/** * 获取当前瞬时电流 * @return 返回获取的电流 */ public String getCurrent() { String[] cmds = {"cat /sys/class/power_supply/battery/current_now"}; ShellUtils.CommandResult rs = ShellUtils.execCommand(cmds, false, true); if (!TextUtils.isEmpty(rs.successMsg)) { return "当前电流:"+rs.successMsg; } else { return "当前电流:" + "unknown"; } } /** * 获取当前Counter * @return 获取的counter */ public String getCounter() { String[] cmds = new String[]{"cat /sys/class/power_supply/battery/charge_counter"}; ShellUtils.CommandResult rs = ShellUtils.execCommand(cmds, false, true); if (!TextUtils.isEmpty(rs.successMsg)) { return "库伦值:" + rs.successMsg; } else { return "库伦值:" + "unknown"; } //Log.e("debug","库伦值:"+rs.successMsg); } /** * 获取GPU的频率 * @return 返回GPU频率 */ public String getGPU() { String[] cmds = new String[]{"cat /sys/class/kgsl/kgsl-3d0/gpuclk"}; ShellUtils.CommandResult rs = ShellUtils.execCommand(cmds, false, true); if (!TextUtils.isEmpty(rs.successMsg)) { return "GPU:" + rs.successMsg+"("+gpuBusy()+")"; } else { return "GPU:" + "unknown"+"("+gpuBusy()+")"; } } /** * DDR的频率和使用率 * @return */ public String clkMeasure() { String[] cmds = new String[]{"cd /d/clk/bimc_clk/","cat clk_measure"}; ShellUtils.CommandResult rs = ShellUtils.execCommand( cmds, false, true); // Log.e("xxxxx",rs.errorMsg); if (!TextUtils.isEmpty(rs.successMsg)) { return "DDR:" + rs.successMsg+"("+meminfo()+")"; } else { return "DDR:" + "unknown"+"("+meminfo()+")"; } } /** * 获取当前DDR使用率 * @return 返回内存使用率% */ public String meminfo() { String[] cmds = new String[]{"cat proc/meminfo"}; ShellUtils.CommandResult rs = ShellUtils.execCommand(cmds, false, true); if (!TextUtils.isEmpty(rs.successMsg)) { String[] tmpList = rs.successMsg.split("kB"); Double total = null, free = null, available = null, cached = null; for (String line : tmpList) { if (line.contains("MemTotal:")) { total = Double.valueOf(Pattern.compile("[^0-9]").matcher(line).replaceAll("")); } if (line.contains("MemFree:")) { free = Double.valueOf(Pattern.compile("[^0-9]").matcher(line).replaceAll("")); } if (line.contains("MemAvailable:")) { available = Double.valueOf(Pattern.compile("[^0-9]").matcher(line).replaceAll("")); } if (line.contains("Cached:")) { cached = Double.valueOf(Pattern.compile("[^0-9]").matcher(line).replaceAll("")); if (total != null && total > 0 && free != null && free > 0) { break; } } } available = 0.00; return String.valueOf(100-mathRound((free + cached) / total * 100)) + "%"; //Log.e("debug","DDR使用率:"+String.valueOf((total-free-available)/total*100)+"%"); } else { return "unknown"; } } /** * CPU使用率、状态、频率 * @return */ private String getCPUStatus() { String cpuStatusString = ""; String cpuOnlineStatus = ""; String cpuFreqStatus = ""; ArrayList currentInfo = takeCpuUsageSnapshot(); int[] cpuUsages = calcCpuUsages(currentInfo, mLastInfo); if (cpuUsages != null && cpuUsages.length > 0) { cpuStatusString = "CPU状态:"+cpuUsages[0] + "%\n"; // Log.d(TAG, "cpuUsages length=" + cpuUsages.length); } else { cpuStatusString = "CPU状态:"+"unknown\n"; //Log.d(TAG, "cpuUsages is null"); } int cpuNum = getCpuNum(); int onlineCPUCount = 1; for (int i=0; i= logfile_switch) { ret += printMsgToFile(priority, tag, msg); } return ret; } public static int selfDefine(String tag, String msg, boolean showOnLog) { if (!showOnLog) { return println(SELF_DEFINE_ONLY_TXT, tag, msg); } else { return println(SELF_DEFINE, tag, msg); } } public static int selfDefine(String tag, String msg) { return selfDefine(tag, msg, false); } }

工具类StreamGobbler:

package com.meitutest.getstat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; public class StreamGobbler extends Thread { InputStream is; String type; OutputStream os; StreamGobbler(InputStream is, String type) { this(is, type, null); } StreamGobbler(InputStream is, String type, OutputStream redirect) { this.is = is; this.type = type; this.os = redirect; } public void run() { try { PrintWriter pw = null; if (os != null) pw = new PrintWriter(os); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) { if (pw != null) pw.println(line); System.out.println(type + ">" + line); } if (pw != null) pw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } } }

新建一个线程,再添加使用一个TimerTask的子类,外加一个输出窗口,让这些方法间隔一段时间执行一次,便可以获取实时获取了。

我这里还添加了一个持久化到本地的txt文件中,便于后续查询。

@Override public void run() { isRun = true; // TestReport.selfDefine(TAG, getCurrent()); for (int i = 0; i < 9; i++) { //当前电流 TestReport.selfDefine(TAG, getCurrent()); sleep(1000); if (i == 8||i= logcat_switch) { ret += android.util.Log.println(priority, tag, msg); } if (priority >= logfile_switch) { ret += printMsgToFile(priority, tag, msg); } return ret; } public static int selfDefine(String tag, String msg, boolean showOnLog) { if (!showOnLog) { return println(SELF_DEFINE_ONLY_TXT, tag, msg); } else { return println(SELF_DEFINE, tag, msg); } } public static int selfDefine(String tag, String msg) { return selfDefine(tag, msg, false); }

成果图:

 

 

需要注意的是: 通过APP获取并且做了一些数据上的处理,以及持久化到sdcard上。这些操作都会使用到CPU的处理、IO的读写等,可能会对获取的数据非常轻微的影响。这个,大家可以自主权衡。我亲测过电流和CPU的一些变化,几乎可以忽略不计。如果对数据结果精度要求较高的话,不建议使用这种方式来获取。大家自行斟酌。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3