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

}

android 获取cpu信息 Android怎么获取cpu信息

other[1] = getTimes();

return other;

Android怎么获取c

  

爱华网本文地址 » http://www.aihuau.com/a/259761/323349603.html

更多阅读

阿里小号怎么获取临时小号 往微信公众号发信息

阿里小号怎么获取临时小号——简介阿里小号可以代替自己的真实号码,保护私隐那阿里小号怎么获得呢?阿里小号怎么获取临时小号——工具/原料通过实名验证的淘宝帐号阿里小号怎么获取临时小号——方法/步骤阿里小号怎么获取临时小号

ASP如何获取HTML表单提交信息 asp防止表单重复提交

在ASP中,有两个很常用的集合,一个是Request.QueryString,另一个是Request.Form。这两个集合可以获取HTML表单提交的信息。Request.QueryStringHTML 表单中的method有两种,一种是get,另外一种是post。当HTML表单的method="get" 时,可以用

对非法获取公民个人信息罪的几点思索 非法获取公民信息证据

胡婧:2014-08-10博和刑事2014年8月8日,上海市第一中级人民法院开庭审理被告人彼特.威廉.汉弗莱(英国籍)和其妻虞英曾(美国籍)涉嫌非法获取公民个人信息案。法院审理后认为,两名被告人从2009年3月至2013年7月期间,多次向他人购买公民个人信

C#中使用ManagementClass获取本机信息 managementclass 引用

C#提供ManagementClass来对机器的信息进行管理,可以通过设定不同的管理类来获得机器的基本信息。下面给出了一些基本的信息的获取方法,包括获取CPU数目,cpu频率,内存大小,硬盘大小。private void GetLocalInfo(){string cpuCount;string

声明:《android 获取cpu信息 Android怎么获取cpu信息》为网友我的温柔只给你分享!如侵犯到您的合法权益请联系我们删除