我正在编写一个android应用程序显示有关cpu,内存等的一些信息。我如何在屏幕上显示theese信息?使用textview还是其他方式?
答案 0 :(得分:2)
您可以使用此功能并在TextView
private float readCPUUsage() {
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine();
String[] toks = load.split(" ");
long idle1 = Long.parseLong(toks[5]);
long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
try {
Thread.sleep(360);
} catch (Exception e) {}
reader.seek(0);
load = reader.readLine();
reader.close();
toks = load.split(" ");
long idle2 = Long.parseLong(toks[5]);
long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
} catch (IOException ex) {
ex.printStackTrace();
}
return 0;
}
有关详情,请参阅How to get Memory usage and CPU usage in android?
<强> 修改 强>
final Runnable r = new Runnable()
{
public void run()
{
tv.setText(" "+readCPUUsage());
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);