Android obtains information about the cpu, memory, version, and power of the system. 1. CPU frequency and CPU information:/proc/cpuinfo and/proc/stat.
By reading the file/proc/cpuinfo system CPU type and other information.
Read/proc/stat all CPU activity information to calculate CPU usage
The following describes how to obtain the CPU frequency through code:
Copy codeThe Code is as follows: package com. orange. cpu;
Import java. io. BufferedReader;
Import java. io. FileNotFoundException;
Import java. io. FileReader;
Import java. io. IOException;
Import java. io. InputStream;
Public class CpuManager {
// Obtain the maximum CPU frequency (unit: KHZ)
// "/System/bin/cat" command line
// "/Sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" Path of the file storing the maximum frequency
Public static String getMaxCpuFreq (){
String result = "";
ProcessBuilder cmd;
Try {
String [] args = {"/system/bin/cat ",
"/Sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq "};
Cmd = new ProcessBuilder (args );
Process process = cmd. start ();
InputStream in = process. getInputStream ();
Byte [] re = new byte [24];
While (in. read (re )! =-1 ){
Result = result + new String (re );
}
In. close ();
} Catch (IOException ex ){
Ex. printStackTrace ();
Result = "N/";
}
Return result. trim ();
}
// Obtain the Minimum CPU frequency (unit: KHZ)
Public static String getMinCpuFreq (){
String result = "";
ProcessBuilder cmd;
Try {
String [] args = {"/system/bin/cat ",
"/Sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq "};
Cmd = new ProcessBuilder (args );
Process process = cmd. start ();
InputStream in = process. getInputStream ();
Byte [] re = new byte [24];
While (in. read (re )! =-1 ){
Result = result + new String (re );
}
In. close ();
} Catch (IOException ex ){
Ex. printStackTrace ();
Result = "N/";
}
Return result. trim ();
}
// Obtain the current CPU frequency in real time (unit: KHZ)
Public static String getCurCpuFreq (){
String result = "N/";
Try {
FileReader fr = new FileReader (
"/Sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq ");
BufferedReader br = new BufferedReader (fr );
String text = br. readLine ();
Result = text. trim ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return result;
}
// Obtain the CPU name
Public static String getCpuName (){
Try {
FileReader fr = new FileReader ("/proc/cpuinfo ");
BufferedReader br = new BufferedReader (fr );
String text = br. readLine ();
String [] array = text. split (": \ s +", 2 );
For (int I = 0; I <array. length; I ++ ){
}
Return array [1];
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return null;
}
}
2. Memory:/proc/meminfo
Copy codeThe Code is as follows: public void getTotalMemory (){
String str1 = "/proc/meminfo ";
String str2 = "";
Try {
FileReader fr = new FileReader (str1 );
BufferedReader localBufferedReader = new BufferedReader (fr, 8192 );
While (str2 = localBufferedReader. readLine ())! = Null ){
Log. I (TAG, "---" + str2 );
}
} Catch (IOException e ){
}
}
3. Rom size
Copy codeThe Code is as follows: public long [] getRomMemroy (){
Long [] romInfo = new long [2];
// Total rom memory
RomInfo [0] = getTotalInternalMemorySize ();
// Available rom memory
File path = Environment. getDataDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long availableBlocks = stat. getAvailableBlocks ();
RomInfo [1] = blockSize * availableBlocks;
GetVersion ();
Return romInfo;
}
Public long getTotalInternalMemorySize (){
File path = Environment. getDataDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long totalBlocks = stat. getBlockCount ();
Return totalBlocks * blockSize;
}
4. sdCard size
Copy codeThe Code is as follows: public long [] getSDCardMemory (){
Long [] sdCardInfo = new long [2];
String state = Environment. getExternalStorageState ();
If (Environment. MEDIA_MOUNTED.equals (state )){
File sdcardDir = Environment. getExternalStorageDirectory ();
StatFs sf = new StatFs (sdcardDir. getPath ());
Long bSize = sf. getBlockSize ();
Long bCount = sf. getBlockCount ();
Long availBlocks = sf. getAvailableBlocks ();
SdCardInfo [0] = bSize * bCount; // total size
SdCardInfo [1] = bSize * availBlocks; // available size
}
Return sdCardInfo;
}
5. battery power
Copy codeThe Code is as follows: private BroadcastReceiver batteryReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
Int level = intent. getIntExtra ("level", 0 );
// Level plus % is the current power
}
};
RegisterReceiver (batteryReceiver, new IntentFilter (Intent. ACTION_BATTERY_CHANGED ));
6. System Version Information
Copy codeThe Code is as follows: public String [] getVersion (){
String [] version = {"null", "null "};
String str1 = "/proc/version ";
String str2;
String [] arrayOfString;
Try {
FileReader localFileReader = new FileReader (str1 );
BufferedReader localBufferedReader = new BufferedReader (
LocalFileReader, 8192 );
Str2 = localBufferedReader. readLine ();
ArrayOfString = str2.split ("\ s + ");
Version [0] = arrayOfString [2]; // KernelVersion
LocalBufferedReader. close ();
} Catch (IOException e ){
}
Version [1] = Build. VERSION. RELEASE; // firmware version
Version [2] = Build. MODEL; // model
Version [3] = Build. DISPLAY; // system version
Return version;
}
7. mac address and boot time
Copy codeThe Code is as follows: public String [] getOtherInfo (){
String [] other = {"null", "null "};
WifiManager wifiManager = (WifiManager) mContext. getSystemService (Context. WIFI_SERVICE );
WifiInfo wifiInfo = wifiManager. getConnectionInfo ();
If (wifiInfo. getMacAddress ()! = Null ){
Other [0] = wifiInfo. getMacAddress ();
} Else {
Other [0] = "Fail ";
}
Other [1] = getTimes ();
Return other;
}
Private String getTimes (){
Long ut = SystemClock. elapsedRealtime ()/1000;
If (ut = 0 ){
Ut = 1;
}
Int m = (int) (ut/60) % 60 );
Int h = (int) (ut/3600 ));
Return h + "" + mContext. getString (R.string.info _ times_hour) + m + ""
+ MContext. getString (R.string.info _ times_minute );
}