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 送给学长毕业祝福语简短