【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用 ...

众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下:

  
  
  
  
  1. PackageManager packageManager = this.getPackageManager(); 
  2. List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);  

 

  
  
  
  
  1. /** 
  2.  * 查询手机内非系统应用 
  3.  * @param context 
  4.  * @return 
  5.  */ 
  6. public static List<PackageInfo> getAllApps(Context context) { 
  7. List<PackageInfo> apps = new ArrayList<PackageInfo>(); 
  8. PackageManager pManager = context.getPackageManager(); 
  9. //获取手机内所有应用 
  10. List<PackageInfo> paklist = pManager.getInstalledPackages(0); 
  11. for (int i = 0; i < paklist.size(); i++) { 
  12. PackageInfo pak = (PackageInfo) paklist.get(i); 
  13. //判断是否为非系统预装的应用程序 
  14. if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { 
  15. // customs applications 
  16. apps.add(pak); 
  17. return apps; 
  18. 获取图片、应用名、包名: 
  19.                     PackageManager pManager = MessageSendActivity.this.getPackageManager(); 
  20.                     List<PackageInfo> appList  = Utils.getAllApps(MessageSendActivity.this); 
  21.                     for(int i=0;i<appList.size();i++) { 
  22.                     PackageInfo pinfo = appList.get(i); 
  23.                     ShareItemInfo shareItem = new ShareItemInfo(); 
  24.                     //set Icon 
  25.                     shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); 
  26.                     //set Application Name 
  27.                     shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString()); 
  28.                     //set Package Name 
  29.                     shareItem.setPackageName(pinfo.applicationInfo.packageName); 
  30.                     }  

获取支持分享的应用的代码:

  
  
  
  
  1. /** 
  2.  * 查询手机内所有支持分享的应用 
  3.  * @param context 
  4.  * @return 
  5.  */ 
  6. public static List<ResolveInfo> getShareApps(Context context){ 
  7.     List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();  
  8.     Intent intent=new Intent(Intent.ACTION_SEND,null);  
  9.     intent.addCategory(Intent.CATEGORY_DEFAULT);  
  10.     intent.setType("text/plain");  
  11.     PackageManager pManager = context.getPackageManager(); 
  12.     mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);  
  13.     return mApps;  
  14. //由于该方法,返回的并不是 PackageInfo 对象。而是 ResolveInfo。因此获取图片、应用名、包名的方法不一样,如下: 
  15.                     PackageManager pManager = MessageSendActivity.this.getPackageManager(); 
  16.                     /*********************查询手机内所有支持分享的应用*********************/ 
  17.                     List<ResolveInfo> resolveList  = Utils.getShareApps(MessageSendActivity.this); 
  18.                     for(int i=0;i<resolveList.size();i++) { 
  19.                     ResolveInfo resolve = resolveList.get(i); 
  20.                     ShareItemInfo shareItem = new ShareItemInfo(); 
  21.                     //set Icon 
  22.                     shareItem.setIcon(resolve.loadIcon(pManager)); 
  23.                     //set Application Name 
  24.                     shareItem.setLabel(resolve.loadLabel(pManager).toString()); 
  25.                     //set Package Name 
  26.                     shareItem.setPackageName(resolve.activityInfo.packageName); 
  27.                     }  

总结:
通过 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()

你可能感兴趣的:(android,移动开发,职场,手机,休闲)