国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

解析Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息的方法詳解

2020-04-11 12:25:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息 1、CPU頻率,CPU信息:/proc/cpuinfo和/proc/stat
通過(guò)讀取文件/proc/cpuinfo系統(tǒng)CPU的類型等多種信息。
讀取/proc/stat 所有CPU活動(dòng)的信息來(lái)計(jì)算CPU使用率

下面我們就來(lái)講講如何通過(guò)代碼來(lái)獲取CPU頻率:

復(fù)制代碼 代碼如下:

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" 存儲(chǔ)最大頻率的文件的路徑
        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();
        }
         // 實(shí)時(shí)獲取CPU當(dāng)前頻率(單位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、內(nèi)存:/proc/meminfo
復(fù)制代碼 代碼如下:

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大小
復(fù)制代碼 代碼如下:

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大小
復(fù)制代碼 代碼如下:

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、電池電量
復(fù)制代碼 代碼如下:

private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){  

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

6、系統(tǒng)的版本信息
復(fù)制代碼 代碼如下:

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("http://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地址和開(kāi)機(jī)時(shí)間
復(fù)制代碼 代碼如下:

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);  


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙岩市| 左云县| 保康县| 仪陇县| 青川县| 吉木乃县| 平昌县| 金沙县| 滨州市| 固安县| 冷水江市| 新河县| 通州区| 手游| 武功县| 广宗县| 贵德县| 昌江| 鹤庆县| 元谋县| 定西市| 北京市| 毕节市| 邢台县| 南雄市| 富阳市| 平昌县| 广宗县| 济南市| 天峨县| 翁牛特旗| 禄丰县| 鹤峰县| 汽车| 灯塔市| 集贤县| 深水埗区| 浠水县| 哈尔滨市| 乌兰浩特市| 江达县|