Explain how Android gets information about CPU information, memory, version, and power of the system _android

Source: Internet
Author: User
Tags readline split trim cpu usage

Android access to system CPU information, memory, version, power and other information 1, CPU frequency, CPU information:/proc/cpuinfo and/proc/stat
By reading the file/proc/cpuinfo the system CPU type and so on a variety of information.
Read information/proc/stat all CPU activity to calculate CPU usage

Let's talk about how to get CPU frequency through code:

Copy Code code 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 {
Get CPU Maximum frequency (unit khz)
The "/system/bin/cat" command line
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" The path to the file that stores 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/a";
}
return Result.trim ();
}
Get CPU minimum 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/a";
}
return Result.trim ();
}
Get CPU current frequency in real time (unit khz)
public static String Getcurcpufreq () {
String result = "N/a";
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;
}
Get 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 Code code 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 Code code 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 Code code 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 charge
Copy Code code 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 charge.
}
};
Registerreceiver (Batteryreceiver, New Intentfilter (intent.action_battery_changed));

6, the system version information
Copy Code code as follows:

Public string[] GetVersion () {
string[] version={"null", "null", "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 Code code 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 to 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.