溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)

發(fā)布時(shí)間:2020-10-21 14:26:37 來(lái)源:腳本之家 閱讀:245 作者:肖旺銀 欄目:移動(dòng)開(kāi)發(fā)

Android獲取儲(chǔ)存信息以及內(nèi)存信息可以用adb命令查看。

adb查看系統(tǒng)內(nèi)存信息以及儲(chǔ)存信息:

命令:adb shell cat /proc/meminfo

Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)

代碼:

 private static final String FILENAME_PROC_MEMINFO = "/proc/meminfo";
/**
 * 獲取手機(jī)內(nèi)存總大小
 * @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+", ""));
      //如果為正整數(shù)就說(shuō)明數(shù)據(jù)正確的,確保在Double.parseDouble中不會(huì)異常
      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";
  }

  /**
   * 獲取手機(jī)剩余內(nèi)存大小
   * @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+", ""));
      //如果為正整數(shù)就說(shuō)明數(shù)據(jù)正確的,確保在Double.parseDouble中不會(huì)異常
      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 獲取手機(jī)儲(chǔ)存大?。?/p>

命令:adb shell cat /proc/partitions

Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)

代碼:

private static final String FILENAME_PROC_EMMCINFO = "/proc/partitions";
 /**
   * 獲取手機(jī)儲(chǔ)存大小
   * @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);
        //如果為正整數(shù)就說(shuō)明數(shù)據(jù)正確的,確保在Double.parseDouble中不會(huì)異常
        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";
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI