关于Android获取系统cpu信息,内存,版本,电量等信息

  • 时间:
  • 浏览:0
  • 来源:少儿教育在线

关于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大小

  复制代码 代码如下:

  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;

  }

  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);

  }

【关于Android获取系统cpu信息,内存,版本,电量等信息】相关文章:

C#获取特定进程CPU和内存使用率08-24

CPU焊接与品牌信息08-22

exe为什么占用内存和CPU08-23

iOS与Android系统优势对比09-14

Android系统比iOS系统更好玩的原因09-19

如何减少网页的内存与CPU占用?08-23

CPU支持什么样的内存类型08-23

管理学信息管理与信息系统专业怎么样06-28

关于IE7降低内存和降低CPU的几个技巧08-23

猜你喜欢

中国男足长期难以稳定打进世界杯的核心原因

中国仅2002年闯入过世界杯正赛,即便2026世界杯亚洲名额扩至8.5个,国足依旧18强赛垫底出局,问题不是球员单一问题,而是青训根基、联赛生态、管理体制、社会环境、人才成长路

2026-07-20

重温青葱岁月:河北软件职业技术学院论坛 hbsi.net

如果你是河北软件职业技术学院(HBSI)的在校生或校友,当校园回忆涌上心头时,不妨打开https://www.hbsi.net/。这个专属论坛自2007年建立以来,已陪伴一代代

2026-04-17

APEAC2026亚洲幼教年会暨展览会

APEAC2026亚洲幼教年会暨展览会2026年4月10-12日    杭州大会展中心2026年4月10-12日  

2026-02-03

2026中国学前教育展(上海CPE幼教展)

作为中国唯一经商务部批准的“国”字头学前教育专业展会,2026中国国际学前教育及装备展览会(CPE中国幼教展)将于2026年10月21日至23日在上海新国际博览中心隆重举行。本

2026-02-03

2026年(第三十一届)中国国际教育巡回展暨2026中国留学论坛

2026年(第三十一届)中国国际教育巡回展暨2026中国留学论坛即将于4月10日启幕。本届中国国际教育巡回展及教育展进校园活动将依次走进北京、成都、郑州、武汉、上海等五大城市。

2026-02-03