获得手机硬件相关----cpu、内存、id

本文介绍了AndroidUtil类,提供了一系列方法用于收集Android系统性能数据,包括CPU核心数、频率,内存大小,屏幕尺寸,以及获取设备唯一标识。通过这些方法,开发者可以深入了解设备的硬件配置,有助于进行性能优化和应用适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.channelsoft.netphone.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;


import a_vcard.android.text.TextUtils;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.graphics.Point;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;


/**
 * <dl>
 * <dt>AndroidUtil.java</dt>
 * <dd>Description:android系统性能相关数据收集工具类</dd>
 * <dd>Copyright: Copyright (C) 2014</dd>
 *
 * <dd>
 * </dl>
 * 
 * @
 */
public class AndroidUtil {
    private static final String TAG = "AndroidUtil";


    private static Point deviceSize = null;


    /**
     * @author: zhaguitao
     * @Title: getNumCores
     * @Description: 获取cpu核心数
     * @return
     * @date: 2014-1-21 下午1:21:26
     */
    public static int getNumCores() {
        // Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }


        try {
            // Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            Log.d(TAG, "CPU Count: " + files.length);
            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e) {
            // Print exception
            Log.d(TAG, "CPU Count: Failed.");
            e.printStackTrace();
            // Default to return 1 core
            return 1;
        }
    }


    /**
     * @author: zhaguitao
     * @Title: getCpuFrequence
     * @Description: 获取cpu频率,单位:KHz
     * @return
     * @date: 2014-1-21 下午3:02:30
     */
    public static String getCpuFrequence() {
        String result = "0";
        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();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            result = reader.readLine();
        } catch (IOException ex) {
            LogUtil.e("IOException", ex);
        }
        if (TextUtils.isEmpty(result)) {
            result = "0";
        }
        return result;
    }


    /**
     * @author: zhaguitao
     * @Title: getMinCpuFreq
     * @Description: 获取cpu最小频率,单位:KHz
     * @return
     * @date: 2014-1-21 下午3:03:36
     */
    public static String getMinCpuFreq() {
        String result = "0";
        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 = "0";
        }
        return result.trim();
    }


    /**
     * @author: zhaguitao
     * @Title: getCurCpuFreq
     * @Description: 获取cpu当前频率,单位:KHz
     * @return
     * @date: 2014-1-21 下午3:03:59
     */
    public static String getCurCpuFreq() {
        String result = "0";
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            br = new BufferedReader(fr);
            String text = br.readLine();
            result = text.trim();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.toString());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }
        return result;
    }


    /**
     * @author: zhaguitao
     * @Title: getFreeMemory
     * @Description: 获取可用运存大小,单位:M
     * @param context
     * @return
     * @date: 2014-1-21 下午3:04:53
     */
    public static float getFreeMemory(Context context) {
        // 获取android当前可用内存大小
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        // mi.availMem; 当前系统的可用内存
        return mi.availMem / (1024 * 1024.0f);
    }


    /**
     * @author: zhaguitao
     * @Title: getTotalMemory
     * @Description: 获取系统总内存大小,单位:M
     * @param context
     * @return
     * @date: 2014-1-21 下午3:06:58
     */
    public static float getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;
        FileReader localFileReader = null;
        BufferedReader localBufferedReader = null;
        try {
            localFileReader = new FileReader(str1);
            localBufferedReader = new BufferedReader(localFileReader, 8192);
            // 读取meminfo第一行,系统总内存大小
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }
            // 获得系统总内存,单位是KB,乘以1024转换为Byte
            initial_memory = Integer.valueOf(arrayOfString[1]).intValue();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } finally {
            try {
                if (localBufferedReader != null) {
                    localBufferedReader.close();
                }
                if (localFileReader != null) {
                    localFileReader.close();
                }
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }
        return initial_memory / 1024.0f;
    }


    public static boolean availableExStorageMemory() {
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {


            /** 获取存储卡路径 */
            File sdcardDir = Environment.getExternalStorageDirectory();
            /** StatFs 看文件系统空间使用情况 */
            StatFs statFs = new StatFs(sdcardDir.getPath());
            /** Block 的 size */
            long blockSize = statFs.getBlockSize();
            /** 总 Block 数量 */
            int totalBlocks = statFs.getBlockCount();
            /** 已使用的 Block 数量 */
            int availableBlocks = statFs.getAvailableBlocks();


            if (availableBlocks * blockSize > 10 * 1024 * 1024) {
                return true;
            }
        }
        return false;
    }


    /**
     * @
     * @Title: getDeviceSize
     * @Description: 获取手机屏幕宽高
     * @param context
     * @return
     * @date: 2014-3-13 上午9:45:55
     */
    @SuppressLint("NewApi")
    public static Point getDeviceSize(Context context) {
        if (deviceSize == null) {
            deviceSize = new Point(0, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                ((WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE))
                        .getDefaultDisplay().getSize(deviceSize);
            } else {
                Display display = ((WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE))
                        .getDefaultDisplay();
                deviceSize.x = display.getWidth();
                deviceSize.y = display.getHeight();
                display = null;
            }
        }
        return deviceSize;
    }


    /** 唯一标志长度够的场合,作为补位用 */
    private static final String UNIQUEID_SUFFIX = "ABCDEFABCDEF";


    /**
     * 获取Android设备的唯一标识字符串
     * 
     * @param context
     * @return
     */
    public static String getDeviceUniqueId(Context context) {
        String uniqueId = "";
        // 1、获取mac地址
        WifiManager wifiMgr = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        if (wifiInfo != null) {
            uniqueId = wifiInfo.getMacAddress();
            Log.d(TAG, "macAddress=" + uniqueId);
        }
        if (!TextUtils.isEmpty(uniqueId)) {
            return uniqueId;
        }


        // 2、获取android_id
        uniqueId = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);
        Log.d(TAG, "androidId=" + uniqueId);
        if (!TextUtils.isEmpty(uniqueId)) {
            return uniqueId;
        }


        // 3、获取imei
        TelephonyManager telephonyMgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        uniqueId = telephonyMgr.getDeviceId();
        Log.d(TAG, "diviceId=" + uniqueId);


        return uniqueId;
    }


    /**
     * 获取电视终端的唯一标识字符串
     * 
     * @return
     */
    public static String getTVUniqueId(Context context) {
        String uniqueId = getDeviceUniqueId(context);
        if (TextUtils.isEmpty(uniqueId)) {
            // 默认的唯一标识
            return "TV_008F0E2B5B3A";
        } else {
            uniqueId = uniqueId.replaceAll(":", "").toUpperCase();
            if (uniqueId.length() < 12) {
                // 补位
                uniqueId = uniqueId
                        + UNIQUEID_SUFFIX.substring(0, 12 - uniqueId.length());
            } else if (uniqueId.length() > 12) {
                // 截取
                uniqueId = uniqueId.substring(uniqueId.length() - 12);
            }
            return "TV_" + uniqueId;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值