Android obtains information about the CPU, memory, version, and power of the system.

Source: Internet
Author: User

1. CPU frequency, 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. Here we will talk about how to obtain the CPU frequency through code:
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 = 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 = 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

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 sizeWww.2cto.com

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

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

Private broadcastreceiver batteryreceiver = new broadcastreceiver (){
 
@ Override
Public void onreceive (context, 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
?
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
?
Public String [] getotherinfo (){
String [] Other = {"null", "null "};
Wifimanager = (wifimanager) mcontext. getsystemservice (context. wifi_service );
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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.