I'm writing an android application displays some info about cpu, memory etc etc.. How can i display theese infos on the screen? With the textview or other way?
Asked
Active
Viewed 2,242 times
2
-
What is your actual problem? Or your problem is that you are not sure where to start? – Marko Lazić May 22 '13 at 12:47
-
My problem is that i can't find a way to display the CPU's info or memory's info in the app. With a toast yes but i want display them on the screen. For example just over a button or in the center of my activity.. – Atlas91 May 22 '13 at 13:01
-
o/p what i am getting is not the correct ... all the time o/p is in the range of 0.825699 to 1.0 , only one app is running on my android phone. I download the free app from google play for the cpu usage check but both o/p is different different. Can you tell me what o/p I'm getting from tv.setText(" "+readCPUUsage()); – Akarsh M Aug 30 '13 at 13:51
1 Answers
2
You can use this function and Show it in 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;
}
For more see How to get Memory usage and CPU usage in android?
Edit
final Runnable r = new Runnable()
{
public void run()
{
tv.setText(" "+readCPUUsage());
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
-
mmh, interesting..But in my case i have a nexus4 so i've got 4 CPUs. However how can i "connect" this function with the textview? – Atlas91 May 22 '13 at 12:59
-
Ok thank you, but what have i to write in the textview? For example?Just a simple example to understand. – Atlas91 May 22 '13 at 13:13
-
o/p what i am getting is not the correct ... all the time o/p is in the range of 0.825699 to 1.0 , only one app is running on my android phone. I download the free app from google play for the cpu usage check but both o/p is different different. Can you tell me what o/p I'm getting from tv.setText(" "+readCPUUsage()); – Akarsh M Aug 30 '13 at 13:50