温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-21 14:26:37 来源:脚本之家 阅读:245 作者:肖旺银 栏目:移动开发

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

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

命令:adb shell cat /proc/meminfo

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

代码:

 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

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

代码:

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";
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI