【发布时间】:2012-02-20 14:29:23
【问题描述】:
谁能帮助我如何通过代码获取 Android 设备的处理器名称、速度和 RAM。
【问题讨论】:
标签: android
谁能帮助我如何通过代码获取 Android 设备的处理器名称、速度和 RAM。
【问题讨论】:
标签: android
您可以像我们通常在 Linux 中一样获得处理器、RAM 和其他硬件相关信息。 从终端我们可以在普通的 Linux 系统中发出这些命令。您不需要为此拥有 root 设备。
$ cat /proc/cpuinfo
同样,您可以在 android 代码中发出这些命令并获得结果。
public void getCpuInfo() {
try {
Process proc = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStream is = proc.getInputStream();
TextView tv = (TextView)findViewById(R.id.tvcmd);
tv.setText(getStringFromInputStream(is));
}
catch (IOException e) {
Log.e(TAG, "------ getCpuInfo " + e.getMessage());
}
}
public void getMemoryInfo() {
try {
Process proc = Runtime.getRuntime().exec("cat /proc/meminfo");
InputStream is = proc.getInputStream();
TextView tv = (TextView)findViewById(R.id.tvcmd);
tv.setText(getStringFromInputStream(is));
}
catch (IOException e) {
Log.e(TAG, "------ getMemoryInfo " + e.getMessage());
}
}
private static String getStringFromInputStream(InputStream is) {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
try {
while((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
}
catch (IOException e) {
Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
}
finally {
if(br != null) {
try {
br.close();
}
catch (IOException e) {
Log.e(TAG, "------ getStringFromInputStream " + e.getMessage());
}
}
}
return sb.toString();
}
【讨论】:
它只能在有根设备或您的应用程序作为系统应用程序运行。
对于想要的信息,您必须查看正在运行的内核,因为我知道这些信息 android系统本身无法获取。
要获取有关 CPU 的信息,您可以读取并解析此文件: /proc/cpuinfo
要获取内存信息,可以读取并解析这个文件: /proc/内存
【讨论】: