Android设备内存和SD卡操作工具类
2014-12-24 14:34
549 查看
package cc.c; import java.io.File; import java.util.List; import android.os.StatFs; import java.io.FileReader; import java.io.IOException; import java.io.BufferedReader; import android.os.Environment; import android.content.Context; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.app.ActivityManager.RunningAppProcessInfo; public class StorageUtil { private static final int ERROR = -1; public static int save_dir = 1; //推断是否已经安装SD卡 public static boolean isSDCardExist() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } //内存清理 //注意权限: //<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> public static void cleanMemory(Context context){ System.out.println("---> 清理前可用内存:"+getAvailMemory(context)+"MB"); ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); RunningAppProcessInfo runningAppProcessInfo=null; List<RunningAppProcessInfo> runningAppProcessInfoList= activityManager.getRunningAppProcesses(); //List<ActivityManager.RunningServiceInfo> serviceInfos = mActivityManager.getRunningServices(100); if (runningAppProcessInfoList != null) { for (int i = 0; i < runningAppProcessInfoList.size(); ++i) { runningAppProcessInfo= runningAppProcessInfoList.get(i); // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE // 的进程即为长时间未使用进程或者空进程 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE // 的进程都是非可见进程,即在后台执行 if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] pkgList = runningAppProcessInfo.pkgList; for (int j = 0; j < pkgList.length; ++j) { System.out.println("===> 正在清理:"+pkgList[j]); activityManager.killBackgroundProcesses(pkgList[j]); } } } } System.out.println("---> 清理后可用内存:"+getAvailMemory(context)+"MB"); } //获取内存总大小 public static long getTotalMemory() { //系统的内存信息文件 String filePath = "/proc/meminfo"; String lineString; String[] stringArray; long totalMemory = 0; try { FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader,1024 * 8); //读取meminfo第一行,获取系统总内存大小 lineString = bufferedReader.readLine(); //依照空格拆分 stringArray = lineString.split("\\s+"); //获得系统总内存,单位KB totalMemory = Integer.valueOf(stringArray[1]).intValue(); bufferedReader.close(); } catch (IOException e) { } return totalMemory / 1024; } //获取可用内存大小 public static long getAvailMemory(Context context) { ActivityManager activityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo memoryInfo = new MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); return memoryInfo.availMem / (1024 * 1024); } //SD卡剩余空间 public static long getAvailableExternalMemorySize() { if (isSDCardExist()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } else { return ERROR; } } //SD卡总空间 public static long getTotalExternalMemorySize() { if (isSDCardExist()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } else { return ERROR; } } //推断SD卡下external_sd目录的总大小 public static long getTotalExternal_SDMemorySize() { if (isSDCardExist()) { File path = Environment.getExternalStorageDirectory(); File externalSD = new File(path.getPath() + "/external_sd"); if (externalSD.exists() && externalSD.isDirectory()) { StatFs stat = new StatFs(path.getPath() + "/external_sd"); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); if (getTotalExternalMemorySize() != -1 && getTotalExternalMemorySize() != totalBlocks * blockSize) { return totalBlocks * blockSize; } else { return ERROR; } } else { return ERROR; } } else { return ERROR; } } //推断SD卡下external_sd目录的可用大小 public static long getAvailableExternal_SDMemorySize() { if (isSDCardExist()) { File path = Environment.getExternalStorageDirectory(); File externalSD = new File(path.getPath() + "/external_sd"); if (externalSD.exists() && externalSD.isDirectory()) { StatFs stat = new StatFs(path.getPath() + "/external_sd"); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); if (getAvailableExternalMemorySize() != -1 && getAvailableExternalMemorySize() != availableBlocks * blockSize) { return availableBlocks * blockSize; } else { return ERROR; } } else { return ERROR; } } else { return ERROR; } } }
相关文章推荐
- Android设备内存和SD卡操作工具类
- Android设备内存和SD卡操作工具类
- Android开发SD卡文件操作工具类
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- Android工具类——处理网络、内存、屏幕等操作
- Android文件操作(SD卡、缓存)工具类
- Android 关于SD卡、机身内存以及分辨率的转换的工具类
- Android文件操作(SD卡、缓存)工具类
- Android文件操作(SD卡、缓存)工具类
- Android SD卡路径问题以及如何获取SDCard 内存
- Android学习之——SD卡操作
- android 获取SD卡的路径和SD卡内存
- Android:系统信息(内存、cpu、sd卡、电量、版本)的获取
- android系统信息(内存,cpu,sd卡,电量,版本)的获取
- android系统信息(内存、cpu、sd卡、电量、版本)获取
- Android SD卡路径问题以及如何获取SDCard 内存
- android 获取SD卡的路径和SD卡内存
- Android操作Uri的工具类
- android sd卡状态、路径、可用空间,内存
- Android SD卡操作 (Environment 状态)