欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android获取系统储存以及内存信息的方法(二)

时间:2021-02-12 08:51:56|栏目:Android代码|点击:

Android获取储存信息以及内存信息可以用adb命令查看。

adb查看系统内存信息以及储存信息:

命令:adb shell cat /proc/meminfo

这里写图片描述

代码:

 private static final String FILENAME_PROC_MEMINFO = "/proc/meminfo";
/**
 * 获取手机内存总大小
 * @return
 */
 public static String getTotalMemorySize() {
    try {
      FileReader fr = new FileReader(FILENAME_PROC_MEMINFO);
      BufferedReader br = new BufferedReader(fr, 2048);
//      String memoryLine = br.readLine();
      String subMemoryLine = "";
      String Line = "";
      while ((Line = br.readLine()) != null)
      {
        if (Line.contains("MemTotal:")){
          subMemoryLine = Line.substring(Line.indexOf("MemTotal:"));
          break;
        }

      }
      br.close();
      Matcher mer = Pattern.compile("^[0-9]+$").matcher(subMemoryLine.replaceAll("\\D+", ""));
      //如果为正整数就说明数据正确的,确保在Double.parseDouble中不会异常
      if (mer.find()) {
        long memSize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) ;
        double mem = (Double.parseDouble(memSize + "")/1024)/1024;
        NumberFormat nf = new DecimalFormat( "0.0 ");
        mem = Double.parseDouble(nf.format(mem));
        //Log.e(LOG_TAG,"=========mem================ " + mem);
        return String.valueOf(mem);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "Unavailable";
  }

  /**
   * 获取手机剩余内存大小
   * @return
   */
  public static String getFreeMemorySize() {
    try {
      FileReader fr = new FileReader(FILENAME_PROC_MEMINFO);
      BufferedReader br = new BufferedReader(fr, 2048);
//      String memoryLine = br.readLine();
      String subMemoryLine = "";
      String Line = "";
      while ((Line = br.readLine()) != null)
      {
        if (Line.contains("MemFree:")){
          subMemoryLine = Line.substring(Line.indexOf("MemFree:"));
          break;
        }

      }
      br.close();
      Matcher mer = Pattern.compile("^[0-9]+$").matcher(subMemoryLine.replaceAll("\\D+", ""));
      //如果为正整数就说明数据正确的,确保在Double.parseDouble中不会异常
      if (mer.find()) {
        long memSize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) ;
        double mem = (Double.parseDouble(memSize + "")/1024)/1024;
        NumberFormat nf = new DecimalFormat( "0.0 ");
        mem = Double.parseDouble(nf.format(mem));
        //Log.e(LOG_TAG,"=========mem================ " + mem);
        return String.valueOf(mem);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "Unavailable";
  }

adb 获取手机储存大小:

命令:adb shell cat /proc/partitions

这里写图片描述

代码:

private static final String FILENAME_PROC_EMMCINFO = "/proc/partitions";
 /**
   * 获取手机储存大小
   * @return
   */
 public static String getEmmcSize() {
    try {
      FileReader fr = new FileReader(FILENAME_PROC_EMMCINFO);
      BufferedReader br = new BufferedReader(fr, 2048);
      String Line = "";
      String EmmcSize = "";
      while ((Line = br.readLine()) != null)
      {
        if (Line.length() >7 && Line.lastIndexOf(" ") > 0) {
          if (Line.substring(Line.lastIndexOf(" ") + 1 , Line.length()).equals("mmcblk0"))
          {

            EmmcSize = removeAllSpace(Line);
            break;
          }
        }

      }
      br.close();
      if (EmmcSize.length() >7 ){
        EmmcSize = (String) EmmcSize.subSequence(4, EmmcSize.length()-7);

        Matcher mer = Pattern.compile("^[0-9]+$").matcher(EmmcSize);
        //如果为正整数就说明数据正确的,确保在Double.parseDouble中不会异常
        if (mer.find()) {
          double mem = (Double.parseDouble(EmmcSize)/1024)/1024;
          NumberFormat nf = new DecimalFormat( "0.0 ");
          mem = Double.parseDouble(nf.format(mem));
          return String.valueOf(mem);
        }
      }
      return "0";
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "Unavailable";
  }

上一篇:android TabHost(选项卡)的使用方法

栏    目:Android代码

下一篇:Android实现完整游戏循环的方法

本文标题:Android获取系统储存以及内存信息的方法(二)

本文地址:http://www.codeinn.net/misctech/61829.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有