文章目录
  1. 1. 电量信息
  2. 2. CPU使用率
  3. 3. 存储信息
    1. 3.1. SD卡信息
    2. 3.2. 内部存储信息
  4. 4. 内存信息

出于毕设客户端的开发需要,想要收集Android设备的内存、CPU、存储和电量信息,通过查找现在这四个信息的获取代码如下:

电量信息

通过广播实现,系统在电池电量信息发送变化时,回向外发送广播。只要注册一个监听Intent.ACTION_BATTERY_CHANGED的广播接收器并声明权限即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void batteryLevel() {  
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
int scale = -1;
int level = -1;
int voltage = -1;
int temperature = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
Log.i("BatteryManager", "level is "+level+"/"+scale+", temp is "+ temperature +", voltage is "+voltage);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
MyApplication.getContext().registerReceiver(batteryLevelReceiver, filter);
}

这里要注意的是在广播接收器BroadcastReceiver的onReceiver方法中的语句只会在接受到系统广播之后才能得到执行,若果刚刚注册这个广播监听器接着就是获取系统电量,那么一定会遇到得到的电量为0等问题,可以参见stackoverflow上这个问题。解决方法应该是先注册广播接收器,一段时间后在获取电量信息。

CPU使用率

主要是借助top命令,这里我只需要用户的cup使用率和系统cup使用率两个参数即可,所以只需输出的第一行如下
User 7%, System 8%, IOW 0%, IRQ 0%
实现的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public void CpuUsage() throws Exception{
String topresult;
Process process = Runtime.getRuntime().exec("top -n 1");

BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
while((topresult = br.readLine())!=null)
{
if(topresult.trim().length()<1){
continue;
}else{
Log.i("CpuUsage", topresult);
String[] cpuInfoStrings = topresult.split(",");
cpuInfo = new HashMap<String,Integer>();
cpuInfo.put("Users",findNumInString(cpuInfoStrings[0]));
cpuInfo.put("sys", findNumInString(cpuInfoStrings[1]));
break;
}
}
}

private int findNumInString(String cpuInfoString){
int percent;
Pattern pattern = Pattern.compile("\\w*\\s(\\d*)\\%");
Matcher matcher = pattern.matcher(cpuInfoString);
if (matcher.find()) {
String temp = matcher.group(1);
percent = Integer.parseInt(temp);
}
else {
percent = -1;
}
return percent;
}

存储信息

存储信息分为SD卡和系统自身的,手机自身除了系统之外的存储空间被称为ExternalStorage,这里要注意这不是值自己的插入的TF卡

SD卡信息

下面是获取SD卡存储空间的代码,主要是获取块数量和每块的大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void SDCardSize() {  
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long blockSize = sf.getBlockSize();
long blockCount = sf.getBlockCount();
long availCount = sf.getAvailableBlocks();
Log.d("" , "block大小:" + blockSize+ ",block数目:" + blockCount+ ",总大小:" +blockSize*blockCount/ 1024 /1024+ "MB" );
totalSizeInSd = blockSize*blockCount/ 1024 /1024;
Log.d("" , "可用的block数目::" + availCount+ ",剩余空间:" + availCount*blockSize/ 1024 /1024+ "MB" );
usefulSizeInSd = availCount*blockSize/ 1024 / 1024;
}
else {
totalSizeInSd = 0L;
usefulSizeInSd = 0L;
}
}

内部存储信息

原理很SD卡一样,都是根据块数量和每块的大小

1
2
3
4
5
6
7
8
9
10
11
public void internalStorageSize(){
File root = Environment.getRootDirectory();
StatFs sf = new StatFs(root.getPath());
long blockSize = sf.getBlockSize();
long blockCount = sf.getBlockCount();
long availCount = sf.getAvailableBlocks();
Log.d("", "block大小:"+ blockSize+",block数目:"+ blockCount+",总大小:"+blockSize*blockCount/1024+"MB");
totalSizeInternal = blockSize*blockCount/1024/1024;
Log.d("", "可用的block数目::"+ availCount+",可用大小:"+ availCount*blockSize/1024+"MB");
usefulSizeInternal = availCount*blockSize/1024/1024;
}

内存信息

内存主要是通过获取系统活动管理器对象(ActivityManager)和ActivityManager.MemoryInfo对象,调用ActivityManager对象的getMemoryInfo方法,将Android内存信息保存在在ActivityManager.MemoryInfo对象对象中

1
2
3
4
5
6
7
8
9
10
public void memoryInfo(){
//获得MemoryInfo对象
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
//获取系统服务信息
ActivityManager myActivityManager = (ActivityManager)MyApplication.getContext().getSystemService(Activity.ACTIVITY_SERVICE);
//获得系统可用内存,保存在MemoryInfo对象上
myActivityManager.getMemoryInfo(memoryInfo);
usefulMemorySize = memoryInfo.availMem / 1024 /1024;
//totalMemorySize = memoryInfo.totalMem;
}

这里的一个小问题是,memoryInfo.totalMem添加之后,Eclipse并没有在该语句周围报错,但却在整个.java文件中报错。查询andriod API发现totalMen成员变量是在API 16之后加入的,但是我这里target API也是大于16的。

文章目录
  1. 1. 电量信息
  2. 2. CPU使用率
  3. 3. 存储信息
    1. 3.1. SD卡信息
    2. 3.2. 内部存储信息
  4. 4. 内存信息