获取系统运行内存(RAM)大小:
public static void getRAMTotalMemorySize(final Context context){
//获得ActivityManager服务的对象
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
//获得MemoryInfo对象
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ;
//获得系统可用内存,保存在MemoryInfo对象上
mActivityManager.getMemoryInfo(memoryInfo) ;
long memSize = memoryInfo.totalMem ;
//字符类型转换
String availMemStr = formateFileSize(context,memSize);
return availMemStr ;
}
获取系统可用运行内存(RAM)大小:
public static void getRAMAvaialbeMemorySize(final Context context){
//获得MemoryInfo对象
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ;
//获得系统可用内存,保存在MemoryInfo对象上
mActivityManager.getMemoryInfo(memoryInfo) ;
long memSize = memoryInfo.availMem ;
//字符类型转换
String availMemStr = formateFileSize(context,memSize);
return availMemStr ;
}
获取系统存储空间(ROM)大小:
public static void getROMTotalSize(final Context context) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
String availMemStr = formateFileSize(context, blockSize * totalBlocks);
return availMemStr ;
}
获取系统可用存储空间(ROM)大小:
public static void getROMAvailableSize(final Context contex) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
String availMemStr = formateFileSize(context, blockSize * availableBlocks);
return availMemStr ;
}
//调用系统函数,字符串转换 long -String KB/MB
public static String formateFileSize(Context context,long size){
return Formatter.formatFileSize(context, size);
}
作者:祥子Gyx