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 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短