获取手机的硬件信息

时间:2021-07-14 17:12:56
获取手机的硬件信息
 
 
public class MainActivity extends AppCompatActivity {    private TextView mTextView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mTextView = (TextView) findViewById(R.id.textViewId);        getPhoneInfo();    }    /**     * 获取手机信息     */    public void getPhoneInfo() {        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);        String mtyb = android.os.Build.BRAND;// 手机品牌        String mtype = android.os.Build.MODEL; // 手机型号        String imei = tm.getDeviceId();   //IMEI        String imsi = tm.getSubscriberId(); //imsi        String numer = tm.getLine1Number(); // 手机号码        String serviceName = tm.getSimOperatorName(); // 运营商        mTextView.setText("品牌: " + mtyb + "\n"                + "型号: " + mtype + "\n"                + "版本: Android " + android.os.Build.VERSION.RELEASE + "\n"                + "IMEI: " + imei + "\n" +                "IMSI: " + imsi + "\n"                );        mTextView.append("内部版本:"+getInner_Ver()+"\n");        mTextView.append("基带版本:"+getBaseband_Ver()+"\n");        mTextView.append("内核版本号:"+getKernelVersion()+"\n");//        mTextView.append("总内存: " + getTotalMemory() + "\n");//        mTextView.append("当前可用内存: " + getAvailMemory() + "\n");//        mTextView.append("CPU名字: " + getCpuName() + "\n");//        mTextView.append("CPU最大频率: " + getMaxCpuFreq() + "\n");//        mTextView.append("CPU最小频率: " + getMinCpuFreq() + "\n");//        mTextView.append("CPU当前频率: " + getCurCpuFreq() + "\n");    }    /**     * 获取手机内存大小     *     * @return     */    private String getTotalMemory() {        String str1 = "/proc/meminfo";// 系统内存信息文件        String str2;        String[] arrayOfString;        long initial_memory = 0;        try {            FileReader localFileReader = new FileReader(str1);            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);            str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小            arrayOfString = str2.split("\\s+");            for (String num : arrayOfString) {                Log.i(str2, num + "\t");            }            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte            localBufferedReader.close();        } catch (IOException e) {        }        return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化    }    /**     * 获取当前可用内存大小     *     * @return     */    private String getAvailMemory() {        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();        am.getMemoryInfo(mi);        return Formatter.formatFileSize(getBaseContext(), mi.availMem);    }    /*CPU最大频率*/    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() + "Hz";    }    // 获取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() + "Hz";    }    // 实时获取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() + "Hz";        } 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;    }    /**     * BASEBAND-VER     * 基带版本     * return String     */    public static String getBaseband_Ver(){        String Version = "";        try {            Class cl = Class.forName("android.os.SystemProperties");            Object invoker = cl.newInstance();            Method m = cl.getMethod("get", new Class[] { String.class,String.class });            Object result = m.invoke(invoker, new Object[]{"gsm.version.baseband", "no message"});// System.out.println(">>>>>>><<<<<<<" +(String)result);            Version = (String)result;        } catch (Exception e) {        }        return Version;    }    /**     * INNER-VER     * 内部版本     * return String     */    public static String getInner_Ver(){        String ver = "" ;        if(android.os.Build.DISPLAY .contains(android.os.Build.VERSION.INCREMENTAL)){            ver = android.os.Build.DISPLAY;        }else{            ver = android.os.Build.VERSION.INCREMENTAL;        }        return ver;    }    /*    * 内核版本号    * */    public static String getKernelVersion() {        String kernelVersion = "";        InputStream inputStream = null;        try {            inputStream = new FileInputStream("/proc/version");        } catch (FileNotFoundException e) {            e.printStackTrace();            return kernelVersion;        }        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 8 * 1024);        String info = "";        String line = "";        try {            while ((line = bufferedReader.readLine()) != null) {                info += line;            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                bufferedReader.close();                inputStream.close();            } catch (IOException e) {                e.printStackTrace();            }        }        try {            if (info != "") {                final String keyword = "version ";                int index = info.indexOf(keyword);                line = info.substring(index + keyword.length());                index = line.indexOf(" ");                kernelVersion = line.substring(0, index);            }        } catch (IndexOutOfBoundsException e) {            e.printStackTrace();        }        return kernelVersion;    }}