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
publicvoidgetTotalMemory(){Stringstr1="/proc/meminfo";Stringstr2="";try{FileReaderfr=newFileReader(str1);BufferedReaderlocalBufferedReader=newBufferedReader(fr,8192);while((str2=localBufferedReader.readLine())!=null){Log.i(TAG,"---"+str2);}}catch(IOExceptione){}}
3、Rom大小
publiclong[]getRomMemroy(){long[]romInfo=newlong[2];//TotalrommemoryromInfo[0]=getTotalInternalMemorySize();//AvailablerommemoryFilepath=Environment.getDataDirectory();StatFsstat=newStatFs(path.getPath());longblockSize=stat.getBlockSize();longavailableBlocks=stat.getAvailableBlocks();romInfo[1]=blockSize*availableBlocks;getVersion();returnromInfo;}publiclonggetTotalInternalMemorySize(){Filepath=Environment.getDataDirectory();StatFsstat=newStatFs(path.getPath());longblockSize=stat.getBlockSize();longtotalBlocks=stat.getBlockCount();returntotalBlocks*blockSize;}
4、sdCard大小
publiclong[]getSDCardMemory(){long[]sdCardInfo=newlong[2];Stringstate=Environment.getExternalStorageState();if(Environment.MEDIA_MOUNTED.equals(state)){FilesdcardDir=Environment.getExternalStorageDirectory();StatFssf=newStatFs(sdcardDir.getPath());longbSize=sf.getBlockSize();longbCount=sf.getBlockCount();longavailBlocks=sf.getAvailableBlocks();sdCardInfo[0]=bSize*bCount;//总大小sdCardInfo[1]=bSize*availBlocks;//可用大小}returnsdCardInfo;}
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、系统的版本信息
publicString[]getVersion(){String[]version={"null","null","null","null"};Stringstr1="/proc/version";Stringstr2;String[]arrayOfString;try{FileReaderlocalFileReader=newFileReader(str1);BufferedReaderlocalBufferedReader=newBufferedReader(localFileReader,8192);str2=localBufferedReader.readLine();arrayOfString=str2.split("\\s+");version[0]=arrayOfString[2];//KernelVersionlocalBufferedReader.close();}catch(IOExceptione){}version[1]=Build.VERSION.RELEASE;//firmwareversionversion[2]=Build.MODEL;//modelversion[3]=Build.DISPLAY;//systemversionreturnversion;}
7、mac地址和开机时间
publicString[]getOtherInfo(){String[]other={"null","null"};WifiManagerwifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);WifiInfowifiInfo=wifiManager.getConnectionInfo();if(wifiInfo.getMacAddress()!=null){other[0]=wifiInfo.getMacAddress();}else{other[0]="Fail";}other[1]=getTimes();returnother;}privateStringgetTimes(){longut=SystemClock.elapsedRealtime()/1000;if(ut==0){ut=1;}intm=(int)((ut/60)%60);inth=(int)((ut/3600));returnh+""+mContext.getString(R.string.info_times_hour)+m+""+mContext.getString(R.string.info_times_minute);}
部分内容转载自:
http://gqdy365.iteye.com/blog/1066113
http://blog.csdn.net/chuxing/article/details/7571547
更多相关文章
- ActionBar简单使用介绍和Tab切换的应用
- Android通过包名获取应用信息
- Android中如何获取字符或者字符串的宽度
- 更改系统时区
- 查看Android设备给每个应用分配的内存大小
- Android使用ListView构造复杂页面,并响应点击事件,通过intent跳转
- 探究为何:在onCreate中通过View.post能获取宽高
- Android(安卓)使用根据手机屏幕来进行比例拉伸接口获取的照片
- android属性系统--应用程序get和set