Android怎么获取cpu信息
Android怎么获取cpu信息
学习Android开发的同学们你们知道怎么获取cpu信息吗?下面由学习啦小编教大家怎么Android怎么获取cpu信息。
Android获取cpu信息的方法
学习啦在线学习网 1、CPU频率,CPU信息:/proc/cpuinfo和/proc/stat
学习啦在线学习网 通过读取文件/proc/cpuinfo系统CPU的类型等多种信息。
读取/proc/stat 所有CPU活动的信息来计算CPU使用率
下面我们就来讲讲如何通过代码来获取CPU频率:
学习啦在线学习网 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 {
学习啦在线学习网 // 获取CPU最大频率(单位KHZ)
// "/system/bin/cat" 命令行
// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
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();
}
学习啦在线学习网 // 获取CPU最小频率(单位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();
}
// 实时获取CPU当前频率(单位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;
}
学习啦在线学习网 // 获取CPU名字
学习啦在线学习网 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、内存:/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大小 www.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大小
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;//总大小
学习啦在线学习网 sdCardInfo[1] = bSize * availBlocks;//可用大小
}
return sdCardInfo;
}
5、电池电量
学习啦在线学习网 private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
@Override
学习啦在线学习网 public void onReceive(Context context, Intent intent) {
学习啦在线学习网 int level = intent.getIntExtra("level", 0);
学习啦在线学习网 // level加%就是当前电量了
}
};
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
6、系统的版本信息
?
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地址和开机时间
?
学习啦在线学习网 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;
Android怎么获取cpu信息相关文章:
}
学习啦在线学习网 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);
}