实时获取Android手机CPU频率

Android手机的CPU的频率信息被保存在 /sys/devices/system/cpu/cpu0/cpufreq 目录下,通过shell命令查看该目录下的文件,如下图
实时获取Android手机CPU频率_第1张图片
其中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


<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的频率。
实时获取Android手机CPU频率_第2张图片

实时获取Android手机CPU频率_第3张图片

你可能感兴趣的:(Android)