Android获取设备CPU核数以及内存大小的方法

统计一下文件个数便可以获得 CPU 核数。
public static int getNumberOfCPUCores() {
 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
 // Gingerbread doesn't support giving a single application access to both cores, but a
 // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
 // chipset and Gingerbread; that can let an app in the background run without impacting
 // the foreground application. But for our purposes, it makes them single core.
 return 1;
 }
 int cores;
 try {
 cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
 } catch (SecurityException e) {
 cores = DEVICEINFO_UNKNOWN;
 } catch (NullPointerException e) {
 cores = DEVICEINFO_UNKNOWN;
 }
 return cores;
}
private static final FileFilter CPU_FILTER = new FileFilter() {
 @Override
 public boolean accept(File pathname) {
 String path = pathname.getName();
 //regex is slow, so checking char by char.
 if (path.startsWith("cpu")) {
  for (int i = 3; i < path.length(); i++) {
  if (path.charAt(i) < '0' || path.charAt(i) > '9') {
   return false;
  }
  }
  return true;
 }
 return false;
 }
};
public static int getNumberOfCPUCores() {
 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
 // Gingerbread doesn't support giving a single application access to both cores, but a
 // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
 // chipset and Gingerbread; that can let an app in the background run without impacting
 // the foreground application. But for our purposes, it makes them single core.
 return 1;
 }
 int cores;
 try {
 cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
 } catch (SecurityException e) {
 cores = DEVICEINFO_UNKNOWN;
 } catch (NullPointerException e) {
 cores = DEVICEINFO_UNKNOWN;
 }
 return cores;
}
private static final FileFilter CPU_FILTER = new FileFilter() {
 @Override
 public boolean accept(File pathname) {
 String path = pathname.getName();
 //regex is slow, so checking char by char.
 if (path.startsWith("cpu")) {
  for (int i = 3; i < path.length(); i++) {
  if (path.charAt(i) < '0' || path.charAt(i) > '9') {
   return false;
  }
  }
  return true;
 }
 return false;
 }
};

获取 CPU 核数

我们都知道,Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。

Android 的 CPU 设备文件位于 /sys/devices/system/cpu/ 目录,文件名的的格式为 cpu\d+。
1 root@generic_x86_64:/sys/devices/system/cpu # ls
2 cpu0
3 cpufreq
4 cpuidle
5 kernel_max
6 modalias
7 offline
8 online
9 possible
10 power
11 present
12 uevent

获取内存大小

如果 SDK 版本大于等于 JELLY_BEAN ,可以通过 ActivityManager 来获取内从大小。

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static long getTotalMemory(Context c) {
 // memInfo.totalMem not supported in pre-Jelly Bean APIs.
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
 ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
 ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
 am.getMemoryInfo(memInfo);
 if (memInfo != null) {
  return memInfo.totalMem;
 } else {
  return DEVICEINFO_UNKNOWN;
 }
 } else {
 long totalMem = DEVICEINFO_UNKNOWN;
 try {
  FileInputStream stream = new FileInputStream("/proc/meminfo");
  try {
  totalMem = parseFileForValue("MemTotal", stream);
  totalMem *= 1024;
  } finally {
  stream.close();
  }
 } catch (IOException e) {
 }
 return totalMem;
 }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值