Android怎样获取CPU使用率

Youritumei_habataku 2011-08-12 10:25:50
Android怎样获取CPU使用率
我的程序:
private String getCpu() {

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);

return result.toString();

}

in.close();

} catch (Exception ex) {

ex.printStackTrace();

}

return result.toString();

}
这只能获取CPU信息,要怎样获取CPU使用率,或者说是CPU负荷程度
...全文
1555 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
天音无象 2012-07-31
  • 打赏
  • 举报
回复
学习了,正好用上
fontlose 2011-08-12
  • 打赏
  • 举报
回复
使用top命令查看
  • 打赏
  • 举报
回复
结贴,这回应该给DrSmart多点分
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 drsmart 的回复:]
解析字符串即可,比如说正则表达式,写一个
[/Quote]
非常谢谢,现已解决。分享下代码:
//CPU 使用率
String Result;
Process p=Runtime.getRuntime().exec("top -n 1");

BufferedReader br=new BufferedReader(new InputStreamReader
(p.getInputStream ()));
while((Result=br.readLine())!=null)
{
if(Result.trim().length()<1){
continue;
}else{
String[] CPUusr = Result.split("%");
tv.append("USER:"+CPUusr[0]+"\n");
String[] CPUusage = CPUusr[0].split("User");
String[] SYSusage = CPUusr[1].split("System");
tv.append("CPU:"+CPUusage[1].trim()+" length:"+CPUusage[1].trim().length()+"\n");
tv.append("SYS:"+SYSusage[1].trim()+" length:"+SYSusage[1].trim().length()+"\n");
tv.append(Result+"\n");
break;
}
}
DrSmart 2011-08-12
  • 打赏
  • 举报
回复
解析字符串即可,比如说正则表达式,写一个
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 drsmart 的回复:]
Runtime.exec 执行下top
[/Quote]
谢谢,but我这个CPU使用率必须在程序中获得,这样我才能获得一个数值,进行比较
DrSmart 2011-08-12
  • 打赏
  • 举报
回复
Runtime.exec 执行下top
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fontlose 的回复:]
使用top命令查看
[/Quote]
谢谢,不过楼上能说详细点吗,还有,我这个CPU使用率必须在程序中获得,这样我才能获得一个数值,进行比较

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧