一、Android 获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
1、通过PackageManager可获取手机端已安装的apk文件的信息,具体代码如下:
PackageManager packageManager = this.getPackageManager(); List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
通过上述方法,可得到手机中安装的所有应用程序,包括手动安装的apk包的信息、、系统预装的应用软件的信息,要区分这两类软件可使用以下方法:
(a) 从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
(b) 判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。
(1)获取应用的代码:
public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); // 获取手机内所有应用 List<PackageInfo> paklist = pManager.getInstalledPackages(0); for (int i = 0; i < paklist.size(); i++) { PackageInfo pak = (PackageInfo) paklist.get(i); // 判断是否为非系统预装的应用程序 if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { apps.add(pak); } } return apps; }
(2)、获取图片、应用名、包名(ShareItemInfo 类是自己写的,忽视吧):
PackageManager pManager = MessageSendActivity.this.getPackageManager(); List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this); for(int i=0;i<appList.size();i++) { PackageInfo pinfo = appList.get(i); shareItem = new ShareItemInfo(); shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString()); shareItem.setPackageName(pinfo.applicationInfo.packageName); }
(3)获取支持分享的应用的代码:
public static List<ResolveInfo> getShareApps(Context context){ List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent=new Intent(Intent.ACTION_SEND,null); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); PackageManager pManager = context.getPackageManager(); mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return mApps; }
由于该方法,返回的并不是PackageInfo 对象。而是ResolveInfo。因此获取图片、应用名、包名的方法不一样,如下:
PackageManager pManager = MessageSendActivity.this.getPackageManager(); List<ResolveInfo> resolveList = Utils.getShareApps(MessageSendActivity.this); for(int i=0;i<resolveList.size();i++) { ResolveInfo resolve = resolveList.get(i); ShareItemInfo shareItem = new ShareItemInfo(); //set Icon shareItem.setIcon(resolve.loadIcon(pManager)); //set Application Name shareItem.setLabel(resolve.loadLabel(pManager).toString()); //set Package Name shareItem.setPackageName(resolve.activityInfo.packageName); }
总结:
通过 PackageInfo 获取具体信息方法:
包名获取方法:packageInfo.packageName
icon获取获取方法:packageManager.getApplicationIcon(applicationInfo)
应用名称获取方法:packageManager.getApplicationLabel(applicationInfo)
使用权限获取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS)
.requestedPermissions
通过 ResolveInfo 获取具体信息方法:
包名获取方法:resolve.activityInfo.packageName
icon获取获取方法:resolve.loadIcon(packageManager)
应用名称获取方法:resolve.loadLabel(packageManager).toString()
二、获取手机可用内存和总内存(手机的内存信息主要在/proc/meminfo文件中,其中第一行是总内存,而剩余内存可通过ActivityManager.MemoryInfo得到):
private String[] getTotalMemory() { String[] result = {"",""}; //1-total 2-avail ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); mActivityManager.getMemoryInfo(mi); long mTotalMem = 0; long mAvailMem = mi.availMem; String str1 = "/proc/meminfo"; String str2; String[] arrayOfString; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024; localBufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } result[0] = Formatter.formatFileSize(this, mTotalMem); result[1] = Formatter.formatFileSize(this, mAvailMem); Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]); return result; }
三、获取手机CPU信息(和内存信息同理,cpu信息可通过读取/proc/cpuinfo文件来得到,其中第一行为cpu型号,第二行为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[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]); 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; }
五、获取屏幕密度三种方法:
// 获取屏幕密度(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px) int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p) Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); // 获取屏幕密度(方法2) DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) float xdpi = dm.xdpi; float ydpi = dm.ydpi; Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi); Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI); screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px) screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px) Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); // 获取屏幕密度(方法3) dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) xdpi = dm.xdpi; ydpi = dm.ydpi; Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi); Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI); int screenWidthDip = dm.widthPixels; // 屏幕宽(dip,如:320dip) int screenHeightDip = dm.heightPixels; // 屏幕宽(dip,如:533dip) Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip); screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕宽(px,如:480px) screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px) Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);