获取Android各类系统相关信息的接口实现代码
来源:互联网 发布:任城区济宁网络问政 编辑:程序博客网 时间:2024/05/21 19:46
/** * 获取系统中所有安装包信息 */public String getAllPackages(Context context){String appList = "";List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);for (PackageInfo packageInfo : packages) {// 这里可以控制是否需要获取系统级别的packageif ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){appList += packageInfo.applicationInfo.loadLabel(getPackageManager()).toString() + ",";}}return appList.substring(0, appList.length()-1);}/** * 获取系统内存信息 */public String getSysMemoryInfo(Context context){long freeMem = 0;long totalMem = 0;ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);MemoryInfo memoryInfo = new MemoryInfo();activityManager.getMemoryInfo(memoryInfo);freeMem = memoryInfo.availMem/1024/1024;//读取指定文件信息 String path = "/proc/meminfo"; String content = null; BufferedReader br = null; try { br = new BufferedReader(new FileReader(path), 8); String line; if ((line = br.readLine()) != null) { content = line; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } // beginIndex int begin = content.indexOf(':'); // endIndex int end = content.indexOf('k'); // 截取字符串信息content = content.substring(begin + 1, end).trim(); totalMem = Integer.parseInt(content)/1024; return freeMem+" "+totalMem;}/** * 获取CPU信息 */ private String getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String cpuInfo=""; //1-cpu型号 //2-cpu频率 String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo = arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo =cpuInfo + arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } return cpuInfo; } /** * 获取MAC地址 */ private String getMacAddress(){ String result = ""; WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); // Log.i(TAG, "macAdd:" + result); return result; } /** * 获取其他手机信息 */ private String getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String imei = mTm.getDeviceId(); String imsi = mTm.getSubscriberId(); String mtype = android.os.Build.MODEL; // 手机型号 String numer = mTm.getLine1Number(); // 手机号码 return imei + " " + imsi + " " + mtype + " " + numer; } /** * 获取屏幕的分辨率 */ private String getWeithAndHeight(){ //这种方式在service中无法使用, DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; //宽 int height = dm.heightPixels; //高 //在service中也能得到高和宽 WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int width0 = mWindowManager.getDefaultDisplay().getWidth(); int height0 = mWindowManager.getDefaultDisplay().getHeight(); return ""+width +" "+height + " " +width0 + " " +height0; }