工作中需要查看 Android 系统的cpu 使用率情况, 以此查看软件性能以及对其它应用的影响,故此写了一个查看cpu 使用率的控件, 为了使用方便, 把它做成了悬浮窗口的形式, 双击可以启动activity。
查看cpu使用率的方式是读取Linux 系统状态文件/proc/stat, 里面有cpu使用的情况,根据单位时间内的cpu使用情况的变化可以计算出此单位时间内cpu的使用率。关于cpu使用率的计算方式,大家可以搜索之, 此处不再给出。 为使用方便, 把cpu使用率的显示封装成了一个view, 继承至Button, 因此可以响应click事件:
public class CpuUsageView extends Button
在xml中的配置如下:
启动一个线程,每秒计算一次cpu使用率,把结果记录在一个list中, 然后发消息到UI线程,UI线程刷新显示:
Thread mThread = new Thread(){
public void run() {
while (mAttached)
{
int user,nice,sys,idle,iowait,irq,softirq;
int all1,all2,idle1,idle2;
BufferedReader bReader = null;
try {
bReader = new BufferedReader(new FileReader("/proc/stat"));
String strTemp = null;
strTemp = bReader.readLine();
String[] listStrings = strTemp.split(" ");
user = Integer.parseInt(listStrings[2]);
nice = Integer.parseInt(listStrings[3]);
sys = Integer.parseInt(listStrings[4]);
idle = Integer.parseInt(listStrings[5]);
iowait = Integer.parseInt(listStrings[6]);
irq = Integer.parseInt(listStrings[7]);
softirq = Integer.parseInt(listStrings[8]);
all1 = user+nice+sys+idle+iowait+irq+softirq;
idle1 = idle;
bReader.close();
Thread.sleep(1000);
bReader = new BufferedReader(new FileReader("/proc/stat"));
strTemp = bReader.readLine();
listStrings = strTemp.split(" ");
user = Integer.parseInt(listStrings[2]);
nice = Integer.parseInt(listStrings[3]);
sys = Integer.parseInt(listStrings[4]);
idle = Integer.parseInt(listStrings[5]);
iowait = Integer.parseInt(listStrings[6]);
irq = Integer.parseInt(listStrings[7]);
softirq = Integer.parseInt(listStrings[8]);
all2 = user+nice+sys+idle+iowait+irq+softirq;
idle2 = idle;
bReader.close();
mCpuUsage = (float)(all2-all1-(idle2-idle1)) / (all2-all1)*100 ;
synchronized (mListUsage) {
mListUsage.add(mCpuUsage);
if(mListUsage.size() > COLUMNS) {
mListUsage.remove(0);
}
}
//Log.e(TAG, "usage : " + mCpuUsage);
mHandler.sendEmptyMessage(1);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
};
然后再CpuUsageView 的onDraw 函数中绘制cpu使用率波形:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(0x77ff0000);
int width = getWidth();
int heigth = getHeight();
float fPecentage = (float)heigth/100;
List listX = new LinkedList();
listX.add(0.0f);
float deltaX = (float)width/10;
for(int i=1; i 1) {
for(int i = 0; i < size; i++) {
path.lineTo(listX.get(i), heigth-mListUsage.get(i)*fPecentage);
}
path.lineTo(width, heigth);
path.moveTo(0, heigth);
canvas.drawPath(path, paint);
}
}
}
为了在任意时候都能够查看cpu使用率, 把CpuUsageView显示到一个悬浮窗当中, 可以自由拖拽,方便使用。这里用到了服务和WindowManager,需要的同学可以参考下源码, 此处不再赘述。
public void onClick(View v)
{
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.e(TAG, "statusBarHeight = "+statusBarHeight);
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, FloatService.class);
intent.putExtra("statusBarHeight", statusBarHeight);
startService(intent);
finish();
}
整个模块非常简单,但是比较有用, 喜欢的请点赞拿走。