Android编程实现获得内存剩余大小与总大小的方法
本文实例讲述了Android编程实现获得内存剩余大小与总大小的方法。分享给大家供大家参考,具体如下:
publicclassmemInfo{
//获得可用的内存
publicstaticlonggetmem_UNUSED(ContextmContext){
longMEM_UNUSED;
//得到ActivityManager
ActivityManageram=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);//创建ActivityManager.MemoryInfo对象ActivityManager.MemoryInfomi=newActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
//取得剩余的内存空间MEM_UNUSED=mi.availMem/1024;
returnMEM_UNUSED;
}
//获得总内存
publicstaticlonggetmem_TOLAL(){
longmTotal;
///proc/meminfo读出的内核信息进行解释
Stringpath="/proc/meminfo";
Stringcontent=null;
BufferedReaderbr=null;
try{
br=newBufferedReader(newFileReader(path),8);
Stringline;
if((line=br.readLine())!=null){
content=line;
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
//beginIndex
intbegin=content.indexOf(':');
//endIndex
intend=content.indexOf('k');
//截取字符串信息content=content.substring(begin+1,end).trim();
mTotal=Integer.parseInt(content);
returnmTotal;
}
}
希望本文所述对大家Android程序设计有所帮助。