实时获取Android手机CPU频率
来源:互联网 发布:ubuntu 17 wubi安装 编辑:程序博客网 时间:2024/04/30 02:30
Android手机的CPU的频率信息被保存在 /sys/devices/system/cpu/cpu0/cpufreq 目录下,通过shell命令查看该目录下的文件,如下图
其中cpuinfo_cur_freq文件保存CPU当前频率,cpuinfo_max_freq保存CPU可运行最大频率,cpuinfo_min_freq保存CPU可运行最小频率。通过定时读取这些文件便可实时获取CPU频率。
1.创建getCPU类,获取CPU频率信息。
import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;public class getCPU { private final static String CurPath = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq";//保存当前CPU频率 //获取当前CPU频率 public static int getCurCPU(){ int result = 0; FileReader fr = null; BufferedReader br = null; try{ fr = new FileReader(CurPath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }finally { if (fr != null) try{ fr.close(); }catch (IOException e){ e.printStackTrace(); } if (br != null) try{ br.close(); }catch (IOException e){ e.printStackTrace(); } } return result; }private final static String MaxPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";//保存CPU可运行最大频率 //获取CPU可运行最大频率 public static int getMaxCPU(){ int result = 0; FileReader fr = null; BufferedReader br = null; try{ fr = new FileReader(MaxPath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }finally { if (fr != null) try{ fr.close(); }catch (IOException e){ e.printStackTrace(); } if (br != null) try{ br.close(); }catch (IOException e){ e.printStackTrace(); } } return result; } private final static String MinPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq";//保存CPU可运行最小频率 //获取CPU可运行最小频率 public static int getMinCPU(){ int result = 0; FileReader fr = null; BufferedReader br = null; try{ fr = new FileReader(MinPath); br = new BufferedReader(fr); String text = br.readLine(); result = Integer.parseInt(text.trim()); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }finally { if (fr != null) try{ fr.close(); }catch (IOException e){ e.printStackTrace(); } if (br != null) try{ br.close(); }catch (IOException e){ e.printStackTrace(); } } return result; }}
2.MainActivity
public class MainActivity extends Activity { private TextView textView1; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.show_CPU); new Thread(){ public void run(){ try{ while (true){ Thread.sleep(30000);//设置时间间隔为30s Message msg = new Message(); msg.what = UPDATE; handler.sendMessage(msg); } }catch (InterruptedException e){ e.printStackTrace(); } } }.start(); } getCPU getcpu = new getCPU(); private static final int UPDATE = 0; private Handler handler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case UPDATE: textView1.setText(""); textView1.append("CPU最大频率为:"+ Integer.toString(getcpu.getMaxCPU()) + "\n"); textView1.append("CPU最小频率为:"+ Integer.toString(getcpu.getMinCPU()) + "\n"); textView1.append("CPU当前频率为:"+ Integer.toString(getcpu.getCurCPU()) + "\n"); break; default: break; } super.handleMessage(msg); } };}
3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/show_CPU" /></LinearLayout>
运行效果如图,可以看出实现了实时获取CPU的频率。
0 0
- 实时获取Android手机CPU频率
- 实时获取Android手机CPU占用率
- Android如何实时监控CPU频率
- Android中实时获取声音频率
- android 如何获取CPU频率(HZ)
- 获取Android设备的CPU频率
- Android获取CPU频率和温度
- android系统手机限制cpu频率让手机省电
- android app获取手机soc频率
- 获取CPU频率
- 获取cpu频率
- 获取cpu频率
- android手机获取cpu信息
- android用户扫盲:手机cpu频率与架构(上篇)~~~~~~~~~~
- 利用ADB固定Android手机的CPU频率
- android 如何获取CPU频率(HZ…
- 获取cpu频率的代码
- Windows下获取CPU频率
- 多进程与多线程的优劣 与 共享内存的同步问题
- 【Computer Graphics】1.直线生成算法(DDA 算法)
- 单链表的逆置
- LeetCode 67. Add Binary
- IBM3650M3硬件升级
- 实时获取Android手机CPU频率
- nginx配置https代理tomcat
- 安卓官方文档学习的知识点记录
- myeclipse 破解
- rm: cannot remove Permission denied 问题解决方法
- sdn
- Class热替换与卸载
- C语言指针定义全解
- 《机器学习实战》——K近邻算法实现