首页 > 程序开发 > 移动开发 > Android >

Android 获取内存信息

2011-11-07

<p> </p><p>import java.io.BufferedReader;import java.io.FileReader;</p><p>import android.app.Activity;import android.app.ActivityManager;import android.os.Bundle;import android.os.Handler;i...

<p> </p><p>

import java.io.BufferedReader;

import java.io.FileReader;</p><p>import android.app.Activity;

import android.app.ActivityManager;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.widget.TextView;</p><p>public class SystemInfo extends Activity {

private TextView textView,textView2;

ActivityManager am;

ActivityManager.MemoryInfo memoryInfo;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.sysinfo);

textView=(TextView) findViewById(R.id.sys_textView1);

textView2=(TextView) findViewById(R.id.sys_textView2);

am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);

memoryInfo=new ActivityManager.MemoryInfo();

am.getMemoryInfo(memoryInfo);

textView2.setText("本机最大内存:"+GetTotalMemory()+"M");

UpdateMemory Update=new UpdateMemory();

Update.start();

}

class UpdateMemory extends Thread{

public void run() {

while(true){

Message message=new Message();

//message.obj=GetAvailableMemory(memoryInfo.availMem);

message.what=1;

handler.sendMessage(message);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Handler handler=new Handler(){</p><p> public void handleMessage(Message msg) {

// TODO Auto-generated method stub

if (msg.what==1) {

textView.setText("本机还有内存:"+GetAvailableMemory(memoryInfo.availMem)+"M");

}

super.handleMessage(msg);

}

};

private Double GetTotalMemory(){

String str1 = "/proc/meminfo";

String str2;

Double memory=0.0 ;

try {

FileReader r=new FileReader(str1);

BufferedReader bufferedRead=new BufferedReader(r, 8192);

str2=bufferedRead.readLine();

System.err.println("str2--------->"+str2);

//arrayOfString = str2.split("//s+");

///String str3=str2.trim();

//System.out.println("----------------------->"+str3);

String str4=str2.substring(str2.length()-9, str2.length()-3);

//System.out.println(str4+" "+str4.length());

memory=Double.parseDouble(str4)/1000;

} catch (Exception e) {

// TODO: handle exception

}

return memory;

}

private double GetAvailableMemory(long num)

{

double M;

M=(num/1024)/1024;

return M;

}

}</p>

\

摘自 伟哥~ YZ

相关文章
最新文章
热点推荐