1. 程式人生 > >實時獲取Android手機CPU頻率

實時獲取Android手機CPU頻率

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的頻率。
這裡寫圖片描述

這裡寫圖片描述