/** * 获取手机信息 */ public void getPhoneInfo() { DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); PackageManager pkgM= getPackageManager(); StringBuilder sb = new StringBuilder(); sb.append("CPU型号 : " + getCpuName() + "\n" ); sb.append("CPU核心数 : " + getNumberOfCPUCores() + "\n" ); sb.append("CPU频率 : " + getMinCpuFreq() + "-"+ getMaxCpuFreq() +" MHZ "+"\n" ); sb.append("屏幕尺寸 : " + metric.heightPixels + " * "+ metric.widthPixels+"\n" ); sb.append("运行内存 : " + getTotalMemory() + "\n"); String totalSDMemory = String.valueOf(getSDCardMemory()[0] / (1024.0 * 1024.0 * 1024.0)); totalSDMemory = totalSDMemory.substring(0, totalSDMemory.indexOf(".") + 3); String availableMemory = String.valueOf(getSDCardMemory()[1] / (1024.0 * 1024.0 * 1024.0)); availableMemory = availableMemory.substring(0, totalSDMemory.indexOf(".") + 3); sb.append("机身存储 : " + totalSDMemory + "G"+ " ; 可用 : "+ availableMemory+"G"+"\n" ); boolean isSupportAccelerate = pkgM.hasSystemFeature("android.hardware.sensor.accelerometer"); sb.append("加速度传感器 : " + isSupport(isSupportAccelerate) + "\n" ); boolean isSupportMagnetic = pkgM.hasSystemFeature("android.hardware.sensor.compass"); sb.append("电子罗盘 : " + isSupport(isSupportMagnetic) + "\n" ); boolean isSupportGyroscope = pkgM.hasSystemFeature("android.hardware.sensor.gyroscope"); sb.append("陀螺仪传感器 : " + isSupport(isSupportGyroscope) + "\n"); boolean isSupportMicrophone = pkgM.hasSystemFeature("android.hardware.microphone"); sb.append("麦克风 : " + isSupport(isSupportMicrophone) + " \n"); sb.append("屏幕密度 : " + metric.density + "\n" ); mTextView.setText(sb.toString()); } public int getNumberOfCPUCores() { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { // Gingerbread doesn't support giving a single application access to both cores, but a // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core // chipset and Gingerbread; that can let an app in the background run without impacting // the foreground application. But for our purposes, it makes them single core. return 1; } int cores; try { cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length; } catch (SecurityException e) { cores = -1; } catch (NullPointerException e) { cores = -1; } return cores; } private final FileFilter CPU_FILTER = new FileFilter() { @Override public boolean accept(File pathname) { String path = pathname.getName(); //regex is slow, so checking char by char. if (path.startsWith("cpu")) { for (int i = 3; i < path.length(); i++) { if (path.charAt(i) < '0' || path.charAt(i) > '9') { return false; } } return true; } return false; } }; private String isSupport(boolean pValue){ return (pValue ? "支持" : "不支持"); } 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; } /** * 获取手机内存大小 * * @return */ private String getTotalMemory() { String str1 = "/proc/meminfo";// 系统内存信息文件 String str2; String[] arrayOfString = null; 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 String.valueOf(Integer.valueOf(arrayOfString[1]).intValue() / 1024) + " M "; } /** * 获取当前可用内存大小 * * @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); } 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"; } int maxFreq = Integer.parseInt(result.trim())/(1024); return String.valueOf(maxFreq); } // 获取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"; } int minFreq = Integer.parseInt(result.trim())/(1024); return String.valueOf(minFreq); } // 实时获取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; } 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; }
private String getScreenSizeOfDevice2() {// 获取屏幕尺寸,英寸 Point point = new Point(); getWindowManager().getDefaultDisplay().getRealSize(point); DisplayMetrics dm = getResources().getDisplayMetrics(); double x = Math.pow(point.x / dm.xdpi, 2); double y = Math.pow(point.y / dm.ydpi, 2); double screenInches = Math.sqrt(x + y); String inches = String.valueOf(screenInches); return inches.substring(0,inches.indexOf(".")+3); }