当前位置: 移动技术网 > IT编程>移动开发>Android > 解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

qqfo,树蛙雨中打伞,现代e路航官网

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

下面我们就来讲讲如何通过代码来获取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);  


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网