安卓获取手机内存,SD卡内存使用状态的方法
2015-07-12 03:32
549 查看
内存状态获取类:
public class MemoryUtils {
public static final int TotalMemory = 0;
public static final int AvailableMemory = 1;
public static final int freeMemory = 2;
public static String GetMemoryinfo(Context context,File pathFile,int MemoryType) {
StatFs statFs=new StatFs(pathFile.getPath());
long blockSize=statFs.getBlockSize();
switch (MemoryType) {
case TotalMemory:
return Formatter.formatFileSize(context, statFs.getBlockCount()*blockSize);
case AvailableMemory:
return Formatter.formatFileSize(context, statFs.getAvailableBlocks()*blockSize);
case freeMemory:
return Formatter.formatFileSize(context, statFs.getFreeBlocks()*blockSize);
default:
return "0";
}
}
}
调用:
public void sdCartClick(View view) {
TextView textView=(TextView)findViewById(R.id.tvinfo);
File file=Environment.getExternalStorageDirectory();
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
public void PhoneDataClick(View view) {
File file=Environment.getDataDirectory();
TextView textView=(TextView)findViewById(R.id.tvinfo);
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
public void DownLoadCacheClick(View view) {
TextView textView=(TextView)findViewById(R.id.tvinfo);
File file=Environment.getDownloadCacheDirectory();
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
public class MemoryUtils {
public static final int TotalMemory = 0;
public static final int AvailableMemory = 1;
public static final int freeMemory = 2;
public static String GetMemoryinfo(Context context,File pathFile,int MemoryType) {
StatFs statFs=new StatFs(pathFile.getPath());
long blockSize=statFs.getBlockSize();
switch (MemoryType) {
case TotalMemory:
return Formatter.formatFileSize(context, statFs.getBlockCount()*blockSize);
case AvailableMemory:
return Formatter.formatFileSize(context, statFs.getAvailableBlocks()*blockSize);
case freeMemory:
return Formatter.formatFileSize(context, statFs.getFreeBlocks()*blockSize);
default:
return "0";
}
}
}
调用:
public void sdCartClick(View view) {
TextView textView=(TextView)findViewById(R.id.tvinfo);
File file=Environment.getExternalStorageDirectory();
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
public void PhoneDataClick(View view) {
File file=Environment.getDataDirectory();
TextView textView=(TextView)findViewById(R.id.tvinfo);
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
public void DownLoadCacheClick(View view) {
TextView textView=(TextView)findViewById(R.id.tvinfo);
File file=Environment.getDownloadCacheDirectory();
String textString="总内存:"+MemoryUtils.GetMemoryinfo(this,file , MemoryUtils.TotalMemory);
textString+="|可用内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.AvailableMemory);
textString+="|空闲内存:"+MemoryUtils.GetMemoryinfo(this,file, MemoryUtils.freeMemory);
textView.setText(textString);
}
相关文章推荐
- win7旗舰版下怎么配置ODBC数据源
- 普通美国人的词汇量
- HTTP 笔记与总结(4 )socket 编程:批量发帖
- 安卓读写文件和路劲注意事项
- POJ-1046
- 微服务架构成功之路
- org.apache.ibatis.binding.BindingException【原因汇总】
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++
- WEB-注意事项
- 在eclipse下如何安装下载好的插件
- 两层Fragment嵌套,外层Fragment切换时内层Fragment不显示内容
- 贾玲版花木兰,文化行业恰到好处的反思
- 小数量宽带用户的福音,Panabit 云计费easyradius 接口隆重发布,PA宽带计费系统
- 安卓handler机制和RunOnUi方法都能修改主线程,两者的区别
- PHP中单引号和双引号的区别
- linux下ftp的server/client的部署及使用
- string.Split函数
- Git 版本管理 add rm commit push pull
- 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块
- mybatis注解方式懒加载失效分析