//第一步 写个SystemMemory类来获取当前可用运行内存和总运行内存:

 

public class SystemMemory {    /**     *   * 获取android当前可用运行内存大小     *   * @param context     *   *     */    public static String getAvailMemory(Context context) {        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();        am.getMemoryInfo(mi);// mi.availMem; 当前系统的可用内存        return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化    }    /**     *   * 获取android总运行内存大小     *   * @param context     *   *     */    public static String getTotalMemory(Context context) {        String str1 = "/proc/meminfo";// 系统内存信息文件        String str2;        String[] arrayOfString;        long initial_memory = 0;        try {            FileReader localFileReader = new FileReader(str1);            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);            str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小            arrayOfString = str2.split("\\s+");            for (String num : arrayOfString) {                Log.i(str2, num + "\t");            }            // 获得系统总内存,单位是KB            int i = Integer.valueOf(arrayOfString[1]).intValue();            //int值乘以1024转换为long类型            initial_memory = new Long((long)i*1024);            localBufferedReader.close();        } catch (IOException e) {        }        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化    }}

//第二步 直接在Activity里调用就行了:

mText = (TextView) findViewById(R.id.mText);//当前可用运行内存String availMemory = SystemMemory.getAvailMemory(this);//当前总运行内存String totalMemory = SystemMemory.getTotalMemory(this);mText.setText("当前可用运行内存"+availMemory+","+"当前总运行内存"+totalMemory);

 

//我的Activity布局就写了个TextView:

<?xml version="1.0" encoding="utf-8"?>    

//--------------------------------------------------------------完-----------------------------------------------------------------------

 

更多相关文章

  1. 安卓开发模拟器运行时报错原因以及解决方法
  2. Android中Handler引起的内存泄露
  3. AndroidManifest.xml Activity 属性说明
  4. Android(安卓)技巧:命令行运行 sqlite3
  5. android中的androidmanifest.xml中元素含义
  6. Flutter 环境第一次运行问题 ProcessException: Process "D:\fl
  7. Android(安卓)获取当前日期 时间
  8. Android进程和线程(Android开发指南--译)
  9. android获取当前文字的总体长度的方法

随机推荐

  1. Android下图形系统
  2. android studio 使用的一些注意,一些报错
  3. android 自定义圆环
  4. 编译Linaro的Android内核有感
  5. android studio解决warning: Ignoring In
  6. Android沙盒开发之系统libc库定制修改
  7. Android(安卓)变脸谁是幕后的魔法师?
  8. android各版本差异--后期慢慢补充
  9. android的 重要控件使用篇
  10. android MediaPlayer播放InputStream解决