欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-23 21:28:10
android获取储存信息以及内存信息可以用adb命令查看。 adb查看系统内存信息以及储存信息: 命令:adb shell cat /proc/meminfo...

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

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