1.内存使用情况,代码如下:
package com.lml.ratetest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.TextView;
public class MemoryActivity extends Activity {
TextView tvMemory;
Handler handler;
UpdateThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tvactivity);
tvMemory = (TextView) findViewById(R.id.tvRate);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 2) {
tvMemory.setText("RAM一共: " + getTotalMemory() + ", "
+ "可用: " + getAvailMemory()+", 已用:"+getUsed());
}
}
};
thread = new UpdateThread();
thread.start();
}
public class UpdateThread extends Thread {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
thread.interrupt();
}
public String getAvailMemory() {
return Formatter.formatFileSize(getBaseContext(), getAvailMemoryByByte());
}
public long getAvailMemoryByByte(){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
public long getTotalMemoryByByte(){
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
arrayOfString = str2.split("\\s+");
for (String num : arrayOfString) {
Log.i(str2, num + "\t");
}
initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();
} catch (IOException e) {
return 0;
}
return initial_memory;
}
public String getTotalMemory() {
return Formatter.formatFileSize(getBaseContext(), getTotalMemoryByByte());// Byte转换为KB或者MB,内存大小规格化
}
public long getUsedByByte(){
long used = getTotalMemoryByByte()-getAvailMemoryByByte();
return used;
}
public String getUsed(){
return Formatter.formatFileSize(getBaseContext(), getUsedByByte());
}
}
2.cpu占用率,代码如下:
package com.lml.ratetest;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class CpuActivity extends Activity {
TextView tvCpu;
long total = 0;
long idle = 0;
double usage = 0;
UpdateThread thread;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tvactivity);
tvCpu = (TextView) findViewById(R.id.tvRate);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 3) {
tvCpu.setText("cpu 占用率 : "+Math.round((Double)msg.obj));
}
}
};
thread = new UpdateThread();
thread.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
thread.interrupt();
}
public class UpdateThread extends Thread {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message msg = new Message();
msg.what = 3;
msg.obj=readUsage();
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public double readUsage() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("/proc/stat")), 1000);
String load = reader.readLine();
reader.close();
String[] toks = load.split(" ");
long currTotal = Long.parseLong(toks[2]) + Long.parseLong(toks[3])
+ Long.parseLong(toks[4]);
long currIdle = Long.parseLong(toks[5]);
if ((currTotal - total) * 100.0f
/ (currTotal - total + currIdle - idle) >= 0) {
this.usage = (currTotal - total) * 100.0f
/ (currTotal - total + currIdle - idle);
} else {
this.usage = 0;
}
this.total = currTotal;
this.idle = currIdle;
} catch (IOException ex) {
return 0.0;
}
return usage;
}
}