android 得到cpu当前运行频率

有时候,我们希望得到cpu的频率,以决定是否需要对cpu的频率进行调整。在google上搜索了一下后,得到了我想要的答案。修改并运行了一下,是可以得到cpu的频率的。它的原理实质是从文件中读取频率的。


原本这段代码是读取最大频率的,只需要把 “/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq“改成

“/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq”就可以得到当前频率了。

//package org.anddev.andengine.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Scanner;
import java.util.regex.MatchResult;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 15:50:31 - 14.07.2010
 */
class SystemUtils {
  private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n";
  private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";
  private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n";



  /**
   * @return in kiloHertz.
   * @throws SystemUtilsException
   */
  public static int getCPUFrequencyMax() throws Exception {
    return SystemUtils.
      readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
  }

  private static int readSystemFileAsInt(final String pSystemFile) throws Exception {
    InputStream in = null;
    try {
      final Process process = new ProcessBuilder(new String[] {
     "/system/bin/cat", pSystemFile }).start();

      in = process.getInputStream();
      final String content = readFully(in);
      return Integer.parseInt(content);
    } catch (final Exception e) {
      throw new Exception(e);
    } 
  }
  public static final String readFully(final InputStream pInputStream) throws IOException {
    final StringBuilder sb = new StringBuilder();
    final Scanner sc = new Scanner(pInputStream);
    while(sc.hasNextLine()) {
      sb.append(sc.nextLine());
    }
    return sb.toString();
  }
  private static MatchResult matchSystemFile(final String pSystemFile, 
final String pPattern, final int pHorizon) throws Exception {
    InputStream in = null;
    try {
      final Process process = new ProcessBuilder(new String[] 
{ "/system/bin/cat", pSystemFile }).start();

      in = process.getInputStream();
      final Scanner scanner = new Scanner(in);

      final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
      if(matchFound) {
        return scanner.match();
      } else {
        throw new Exception();
      }
    } catch (final IOException e) {
      throw new Exception(e);
    } 
      
  }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值