i'm making live wallpaper displays overall cpu usage percentage. i'm using code 1 of questions here. works, wallpaper freezes few seconds because of thread.sleep(). , if remove that, doesn't display correct values. how cpu usage, id doesn't interfere live wallapaper?
private float readusage() { 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; }
Comments
Post a Comment