在Android中获取内存使用情况有什么API可以让我们使用Android的CPU或内存吗?我尝试了一种代码,如下所示:package com.infostretch.mainactivity;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class CPULoad {
long total = 0;
long idle = 0;
float usage = 0;
public CPULoad()
{
readUsage();
}
public float getUsage()
{
readUsage();
return usage;
}
private void readUsage()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")), 1000);
String load = reader.readLine();
reader.close();
String[] toks = load.split(" ");
long currTotal = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]);
long currIdle = Long.parseLong(toks[5]);
this.usage = (currTotal - total) * 100.0f / (currTotal - total + currIdle - idle);
this.total = currTotal;
this.idle = currIdle;
}
catch(IOException ex)
{
ex.printStackTrace();
}
}}这样做对吗?
- 3 回答
- 0 关注
- 471 浏览
添加回答
举报
0/150
提交
取消