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程序设计有所帮助。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短