解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解

2016-02-19 09:57 27 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解,赶紧看过来吧!

【 tulaoshi.com - 编程语言 】

Android获取系统cpu信息,内存,版本,电量等信息 1、CPU频率,CPU信息:/proc/cpuinfo和/proc/stat
通过读取文件/proc/cpuinfo系统CPU的类型等多种信息。
读取/proc/stat 所有CPU活动的信息来计算CPU使用率

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

下面我们就来讲讲如何通过代码来获取CPU频率:
代码如下:

package com.orange.cpu;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class CpuManager {
        // 获取CPU最大频率(单位KHZ)
     // "/system/bin/cat" 命令行
     // "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
        public static String getMaxCpuFreq() {
                String result = "";
                ProcessBuilder cmd;
                try {
                        String[] args = { "/system/bin/cat",
                                        "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
                        cmd = new ProcessBuilder(args);
                        Process process = cmd.start();
                        InputStream in = process.getInputStream();
                        byte[] re = new byte[24];
                        while (in.read(re) != -1) {
                                result = result + new String(re);
                        }
                        in.close();
                } catch (IOException ex) {
                        ex.printStackTrace();
                        result = "N/A";
                }
                return result.trim();
        }
         // 获取CPU最小频率(单位KHZ)
        public static String getMinCpuFreq() {
                String result = "";
                ProcessBuilder cmd;
                try {
                        String[] args = { "/system/bin/cat",
                                        "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
                        cmd = new ProcessBuilder(args);
                        Process process = cmd.start();
                        InputStream in = process.getInputStream();
                        byte[] re = new byte[24];
                        while (in.read(re) != -1) {
                                result = result + new String(re);
                        }
                        in.close();
                } catch (IOException ex) {
                        ex.printStackTrace();
                        result = "N/A";
                }
                return result.trim();
        }
         // 实时获取CPU当前频率(单位KHZ)
        public static String getCurCpuFreq() {
                String result = "N/A";
                try {
                        FileReader fr = new FileReader(
                                        "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
                        BufferedReader br = new BufferedReader(fr);
                        String text = br.readLine();
                        result = text.trim();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return result;
        }
        // 获取CPU名字
        public static String getCpuName() {
                try {
                        FileReader fr = new FileReader("/proc/cpuinfo");
                        BufferedReader br = new BufferedReader(fr);
                        String text = br.readLine();
                        String[] array = text.split(":s+", 2);
                        for (int i = 0; i array.length; i++) {
                        }
                        return array[1];
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }
}

2、内存:/proc/meminfo
代码如下:

public void getTotalMemory() {  
        String str1 = "/proc/meminfo";  
        String str2="";  
        try {  
            FileReader fr = new FileReader(str1);  
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);  
            while ((str2 = localBufferedReader.readLine()) != null) {  
                Log.i(TAG, "---" + str2);  
            }  
        } catch (IOException e) {  
        }  
    }  

3、Rom大小
代码如下:

public long[] getRomMemroy() {  
        long[] romInfo = new long[2];  
        //Total rom memory  
        romInfo[0] = getTotalInternalMemorySize();  

        //Available rom memory  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long availableBlocks = stat.getAvailableBlocks();  
        romInfo[1] = blockSize * availableBlocks;  
        getVersion();  
        return romInfo;  
    }  

    public long getTotalInternalMemorySize() {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();  
        return totalBlocks * blockSize;  
    } 

4、sdCard大小
代码如下:

public long[] getSDCardMemory() {  
        long[] sdCardInfo=new long[2];  
        String state = Environment.getExternalStorageState();  
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            File sdcardDir = Environment.getExternalStorageDirectory();  
            StatFs sf = new StatFs(sdcardDir.getPath());  
            long bSize = sf.getBlockSize();  
            long bCount = sf.getBlockCount();  
            long availBlocks = sf.getAvailableBlocks();  

            sdCardInfo[0] = bSize * bCount;//总大小  
            sdCardInfo[1] = bSize * availBlocks;//可用大小  
        }  
        return sdCardInfo;  
    }  

5、电池电量
代码如下:

private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){  

        @Override 
        public void onReceive(Context context, Intent intent) {  
            int level = intent.getIntExtra("level", 0);  
            //  level加%就是当前电量了  
    }  
    };  
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

6、系统的版本信息
代码如下:

public String[] getVersion(){  
    String[] version={"null","null","null","null"};  
    String str1 = "/proc/version";  
    String str2;  
    String[] arrayOfString;  
    try {  
        FileReader localFileReader = new FileReader(str1);  
        BufferedReader localBufferedReader = new BufferedReader(  
                localFileReader, 8192);  
        str2 = localBufferedReader.readLine();  
        arrayOfString = str2.split("s+");  
        version[0]=arrayOfString[2];//KernelVersion  
        localBufferedReader.close();  
    } catch (IOException e) {  
    }  
    version[1] = Build.VERSION.RELEASE;// firmware version  
    version[2]=Build.MODEL;//model  
    version[3]=Build.DISPLAY;//system version  
    return version;  


7、mac地址和开机时间
代码如下:

public String[] getOtherInfo(){  
    String[] other={"null","null"};  
       WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);  
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
       if(wifiInfo.getMacAddress()!=null){  
        other[0]=wifiInfo.getMacAddress();  
    } else {  
        other[0] = "Fail";  
    }  
    other[1] = getTimes();  
       return other;  
}  
private String getTimes() {  
    long ut = SystemClock.elapsedRealtime() / 1000;  
    if (ut == 0) {  
        ut = 1;  
    }  
    int m = (int) ((ut / 60) % 60);  
    int h = (int) ((ut / 3600));  
    return h + " " + mContext.getString(R.string.info_times_hour) + m + " " 
            + mContext.getString(R.string.info_times_minute);  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1592784.html

延伸阅读
Delphi以其优良的可视化编程,灵活的Windows API接口,丰富的底层操作越来越受到编程爱好者的青睐。 在Delphi中,通过调用Windows API,可以很方便地获取系统信息,这有助于我们编写出更好的Windows应用程序。以下程序在Delphi3.0 For Windows 9x下编译通过。 一、 用GetDriveType函数获取磁盘信息 Lbl_DriveType:Tlabel; ...
System.Diagnostics.StackTrace可以看到很多运行时当前堆栈中有用的信息,权威参考 http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stacktrace.aspx 开始我是为了得到webservice中当前执行的方法的名称而找到的这个东西。 下面代码演示了,一个类中方法的之间的调用次序 using System; using System.Collection...
标签: Web开发
如果我们需要通过触发事件得到数据行的信息,可以用脚本了来实现,下面是单击数据行得到行信息的代码: function db(index) {     var str = new String("");     var curTRObj = this.Rows[index].Control;     //var column=this.column.lengh;     for ...
//错误处理,显示原因 void __fastcall TSerialPort::ProcessErrorMessage(char* ErrorText) { char ErrorMsg[400]; LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER //自动分配消息缓冲区 FORMAT_MESSAGE_FROM_SYSTEM, //从系统获取信息 NULL,GetLastError(), //获取错误信息标识 MAKELAN...
标签: word
如何查看Word版本信息   1、打开Word主界面,右键单击帮助按钮,然后选择自定义快速访问工具栏选项,如下图所示: 2、进入选项框,选择资源选项,然后点击关于Microsoft Office Word 2007后方的关于选项,如下图所示: 3、即可进行查看更多关于Word 2007版本信息,如下图所示: Word文档中的文字格式   ...

经验教程

98

收藏

84
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部