展会信息港展会大全

Android中查看CPU和内存
来源:互联网   发布日期:2016-01-14 12:32:56   浏览:2522次  

导读:Android系统使用的是linux内核,那么我们可以在程序中去调用cat /proc/meminfo和cat /proc/cpuino来查看内存和CPU的情况,下面是程序:public class CpuSpeed extends Activity { /** Called when th......

Android系统使用的是linux内核,那么我们可以在程序中去调用cat /proc/meminfo和cat /proc/cpuino来查看内存和CPU的情况,下面是程序:

public class CpuSpeed extends Activity {

/** Called when the activity is first created. */

private TextView cpuInfo;

private TextView memoryInfo;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

cpuInfo = (TextView) findViewById(R.id.cpuinfo);

cpuInfo.setText(getCPUinfo());

memoryInfo = (TextView) findViewById(R.id.memoryinfo);

memoryInfo.setText(getMemoryInfo());

}

private String getMemoryInfo() {

ProcessBuilder cmd;

String result = new String();

try {

String[] args = { "/system/bin/cat", "/proc/meminfo" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[1024];

while (in.read(re) != -1) {

System.out.println(new String(re));

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return result;

}

private String getCPUinfo() {

ProcessBuilder cmd;

String result = "";

try {

String[] args = { "/system/bin/cat", "/proc/cpuinfo" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[1024];

while (in.read(re) != -1) {

System.out.println(new String(re));

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return result;

}

}

其实核心无非就是ProcessBuilder的运用,去启动命令行去读操作系统。

String[] args = {"/system/bin/cat", "/proc/cpuinfo"};

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

然后再IO输入流读入就可以了。

赞助本站

人工智能实验室

相关热词: 查看 CPU 内存

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港