देख पूरा CPU उपयोग के लिए (प्रत्येक प्रक्रिया के लिए नहीं) आप का उपयोग कर सकते हैं:
/**
*
* @return integer Array with 4 elements: user, system, idle and other cpu
* usage in percentage.
*/
private int[] getCpuUsageStatistic() {
String tempString = executeTop();
tempString = tempString.replaceAll(",", "");
tempString = tempString.replaceAll("User", "");
tempString = tempString.replaceAll("System", "");
tempString = tempString.replaceAll("IOW", "");
tempString = tempString.replaceAll("IRQ", "");
tempString = tempString.replaceAll("%", "");
for (int i = 0; i < 10; i++) {
tempString = tempString.replaceAll(" ", " ");
}
tempString = tempString.trim();
String[] myString = tempString.split(" ");
int[] cpuUsageAsInt = new int[myString.length];
for (int i = 0; i < myString.length; i++) {
myString[i] = myString[i].trim();
cpuUsageAsInt[i] = Integer.parseInt(myString[i]);
}
return cpuUsageAsInt;
}
private String executeTop() {
java.lang.Process p = null;
BufferedReader in = null;
String returnString = null;
try {
p = Runtime.getRuntime().exec("top -n 1");
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (returnString == null || returnString.contentEquals("")) {
returnString = in.readLine();
}
} catch (IOException e) {
Log.e("executeTop", "error in getting first line of top");
e.printStackTrace();
} finally {
try {
in.close();
p.destroy();
} catch (IOException e) {
Log.e("executeTop",
"error in closing and destroying top process");
e.printStackTrace();
}
}
return returnString;
}
इसके साथ मज़े :)
स्रोत
2012-04-22 16:14:20
कोई CPU उपयोग या समय की जानकारी पाया जा सकता है वहां (या मैंने इसे याद किया?), अन्य चीजों के बीच केवल स्मृति उपयोग। CPU उपयोग के लिए – yuku
, आप CPUGauge.cpp का उल्लेख कर सकते हैं, मुझे लगता है कि इस तरह की जानकारी के लिए "सार्वजनिक एपीआई" नहीं है। या आपको अपने आप से "/ proc/stat" पार्स करना होगा। –
संदर्भ के लिए, यहां CPUGauge https://android.googlesource.com/platform/frameworks/native/+/a6938ba/libs/surfaceflinger/CPUGauge.cpp का स्रोत है –