package com.guo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class ReadSystemMemory extends Activity {
public static final int REFRESH = 0x000001;
public TextView tv = null;
long total = 0;
long idle = 0;
double usage = 0;
public void CPULoad( )
{
readUsage( );
}
public void 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]);
this.usage =(currTotal - total) * 100.0f / (currTotal - total + currIdle - idle);
this.total = currTotal;
this.idle = currIdle;
}
catch( IOException ex )
{
ex.printStackTrace();
}
}
public Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv = (TextView) findViewById(R.id.system_memory);
if (msg.what == ReadSystemMemory.REFRESH) {
if(tv!=null)
tv.setText("mTotle: " + this.getTotalMemory() + ", " + "midle: "
+ this.getAvailMemory()+","+"cusage:"+ Math.round(this.getUsage())+"%");
}
}
public double getUsage( )
{
readUsage( );
return usage;
}
public String getAvailMemory() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
//mi.availMem; 当前系统的可用内存
return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
}
public String getTotalMemory() {
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 Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
tv = (TextView) findViewById(R.id.system_memory);
new MyThread().start();
}
public class MyThread extends Thread {
public void run() {
while (true) {
Message msg = new Message();
msg.what = REFRESH;
msg.obj = this;
mHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ReadSystemMemory.zip (26个子文件)
ReadSystemMemory
AndroidManifest.xml 1KB
res
layout
main.xml 415B
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 3KB
drawable-hdpi
icon.png 4KB
values
strings.xml 183B
hs_err_pid3620.log 4KB
src
com
guo
ReadSystemMemory.java 4KB
bin
com
guo
ReadSystemMemory.class 2KB
R.class 454B
R$layout.class 361B
R$attr.class 307B
R$id.class 358B
R$string.class 394B
ReadSystemMemory$1.class 3KB
ReadSystemMemory$MyThread.class 1KB
R$drawable.class 367B
ReadSystemMemory.apk 16KB
classes.dex 6KB
resources.ap_ 11KB
.classpath 280B
assets
default.properties 364B
hs_err_pid2744.log 4KB
.settings
org.eclipse.jdt.core.prefs 208B
.project 852B
gen
com
guo
R.java 731B
共 26 条
- 1
资源评论
- zhaogu62883222012-09-19蛮好用的,简单方便。
- Hoager2013-02-27蛮好用的,简单方便。可惜不是我想要的东西。
- flcsoft2012-12-31蛮好用,简单方便
- 别调戏我2012-12-09蛮好用的,简单方便。
guoziliangking
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功