天天看點

android 擷取應用程式包名,圖示,入口Activity類

最近老是用到關于程式包名,圖示問題,現總結如下:

[java]  view plain copy

  1. PackageInfo info;    
  2.         try {    
  3.             info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);    
  4.             // 目前應用的版本名稱    
  5.             String versionName = info.versionName;    
  6.             // 目前版本的版本号    
  7.             int versionCode = info.versionCode;    
  8.             // 目前版本的包名    
  9.             String packageNames = info.packageName;    
  10.         } catch (NameNotFoundException e) {    
  11.             e.printStackTrace();    
  12.         }    

另一種方法:

[java]  view plain copy

  1. public class Info {    
  2.     Context context ;    
  3.     PackageManager pm ;    
  4.     public Info(Context context) {    
  5.         this.context = context;    
  6.         pm = context.getPackageManager();    
  7.     }    
  8.     public Drawable getAppIcon(String packname){    
  9.       try {    
  10.              ApplicationInfo info = pm.getApplicationInfo(packname, 0);     
  11.              return info.loadIcon(pm);    
  12.         } catch (NameNotFoundException e) {    
  13.             // TODO Auto-generated catch block    
  14.             e.printStackTrace();    
  15.         }    
  16.     }    
  17.     public String getAppVersion(String packname){    
  18.           try {    
  19.               PackageInfo packinfo = pm.getPackageInfo(packname, 0);    
  20.               return packinfo.versionName;    
  21.             } catch (NameNotFoundException e) {    
  22.                 e.printStackTrace();    
  23.             }    
  24.     }    
  25.     public String getAppName(String packname){    
  26.           try {    
  27.                  ApplicationInfo info = pm.getApplicationInfo(packname, 0);     
  28.                  return info.loadLabel(pm).toString();    
  29.             } catch (NameNotFoundException e) {    
  30.                 // TODO Auto-generated catch block    
  31.                 e.printStackTrace();    
  32.             }    
  33.     }    
  34.     public String[] getAppPremission(String packname){    
  35.           try {    
  36.               PackageInfo packinfo =    pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);    
  37.               //擷取到所有的權限     
  38.               return packinfo.requestedPermissions;    
  39.             } catch (NameNotFoundException e) {    
  40.                 e.printStackTrace();    
  41.             }    
  42.     }    
  43.     public String getAppSignature(String packname){    
  44.           try {    
  45.               PackageInfo packinfo =    pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);    
  46.               //擷取到所有的權限     
  47.               return packinfo.signatures[0].toCharsString();    
  48.             } catch (NameNotFoundException e) {    
  49.                 e.printStackTrace();    
  50.             }    
  51.     }    
  52. http://blog.csdn.net/yudajun/article/details/7855270