安卓獲取設備信息

開發中我們經常需要用到一些設備的信息,接下來直接給大家貼出一個常用的獲取設備信息的工具類。

public class DeviceInfoUtils {


    /**
     * 獲取當前手機系統語言。
     *
     * @return 返回當前系統語言。例如:當前設置的是“中文-中國”,則返回“zh-CN”
     */
    public static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * 獲取當前系統上的語言列表(Locale列表)
     */
    public static Locale[] getSystemLanguageList() {
        return Locale.getAvailableLocales();
    }

    /**
     * 獲取當前手機系統版本號
     */
    public static String getSystemVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 獲取手機型號
     */
    public static String getSystemModel() {
        return Build.MODEL;
    }

    /**
     * 獲取設備寬度(px)
     */
    public static int getDeviceWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 獲取設備高度(px)
     */
    public static int getDeviceHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 獲取手機IMEI(需要“android.permission.READ_PHONE_STATE”權限)
     */
    @SuppressLint("MissingPermission")
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
        if (tm != null) {

            return tm.getDeviceId();
        }
        return null;
    }

    /**
     * 獲取廠商名
     */
    public static String getDeviceManufacturer() {
        return Build.MANUFACTURER;
    }

    /**
     * 獲取產品名
     */
    public static String getDeviceProduct() {
        return Build.PRODUCT;
    }

    /**
     * 獲取手機品牌
     */
    public static String getDeviceBrand() {
        return Build.BRAND;
    }

    /**
     * 獲取手機型號
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }

    /**
     * 獲取手機主板名
     */
    public static String getDeviceBoard() {
        return Build.BOARD;
    }

    /**
     * 設備名
     */
    public static String getDeviceDevice() {
        return Build.DEVICE;
    }

    /**
     * 手機Fingerprint標識
     * fingerprit 信息
     */
    public static String getDeviceFubgerprint() {
        return Build.FINGERPRINT;
    }

    /**
     * 硬件名
     */
    public static String getDeviceHardware() {
        return Build.HARDWARE;
    }

    /**
     * 主機
     */
    public static String getDeviceHost() {
        return Build.HOST;
    }

    /**
     * 顯示ID
     */
    public static String getDeviceDisplay() {
        return Build.DISPLAY;
    }

    /**
     * ID
     */
    public static String getDeviceId() {
        return Build.ID;
    }

    /**
     * 獲取手機用戶名
     */
    public static String getDeviceUser() {
        return Build.USER;
    }

    /**
     * 獲取手機 硬件序列號
     */
    public static String getDeviceSerial() {
        return Build.SERIAL;
    }

    /**
     * 獲取手機Android系統SDK
     */
    public static int getDeviceSDK() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * Codename
     */
    public static String getDeviceCodename() {
        return Build.VERSION.CODENAME;
    }

    /**
     * Bootloader
     */
    public static String getDeviceBootloader() {
        return Build.BOOTLOADER;
    }

    /**
     * 版本類型
     */
    public static String getType() {
        return Build.TYPE;
    }

    /**
     * 安全patch 時間
     */
    public static String getSecurityPatch() {
        return Build.VERSION.SECURITY_PATCH;
    }


    /** 獲取CPU架構
     *
     * @return
     */
    public static String getFrameworkName() {
        return Build.CPU_ABI;
    }

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

    /**cpu核心數
     *
     * @return
     */
    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());
            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e)
        {
            // Default to return 1 core
            return 1;
        }
    }


    /**獲取當前CPU頻率
     *
     * @return
     */
    public static int getCurCPU(){
        int 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 = 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;
    }

    /**
     * 是否root
     * @return
     */
    public static boolean isSuEnable() {
        File file = null;
        String[] paths = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/", "/su/bin/"};
        try {
            for (String path : paths) {
                file = new File(path + "su");
                if (file.exists() && file.canExecute()) {
                    return true;
                }
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
        return false;

    }

    /**
     * 獲取手機本地ip
     * @return
     */
    public static String getLocalIpAddress() {
        StringBuilder sbIpAddress = new StringBuilder("");
        try {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            int one = 0;
            int two = 0;
            while (en.hasMoreElements()) {
                NetworkInterface ni = en.nextElement();
                one++;
                Enumeration<InetAddress> enIp = ni.getInetAddresses();
                while (enIp.hasMoreElements()) {
                    InetAddress inet = enIp.nextElement();
                    two++;
                    if (!inet.isLoopbackAddress()
                            && (inet instanceof Inet4Address)) {
                        //                        return inet.getHostAddress().toString();
                        sbIpAddress.append( inet.getHostAddress().toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sbIpAddress.toString();
    }
}

(裏面獲取IMEI號只在android10版本以下有用,10以上由於權限問題獲取不到了)

會不定時更新安卓相關知識,喜歡的給點一下關注,感謝大家支持!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章