android 获取应用程序包名,图标,入口Activity类

最近老是用到关于程序包名,图标问题,现总结如下:

[java] view plaincopy
  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 plaincopy
  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.     /*  
  9.      * 获取程序 图标  
  10.      */    
  11.     public Drawable getAppIcon(String packname){    
  12.       try {    
  13.              ApplicationInfo info = pm.getApplicationInfo(packname, 0);     
  14.              return info.loadIcon(pm);    
  15.         } catch (NameNotFoundException e) {    
  16.             // TODO Auto-generated catch block    
  17.             e.printStackTrace();    
  18.               
  19.         }    
  20.     }    
  21.         
  22.     /*  
  23.      *获取程序的版本号    
  24.      */    
  25.     public String getAppVersion(String packname){    
  26.             
  27.           try {    
  28.               PackageInfo packinfo = pm.getPackageInfo(packname, 0);    
  29.               return packinfo.versionName;    
  30.             } catch (NameNotFoundException e) {    
  31.                 e.printStackTrace();    
  32.                    
  33.             }    
  34.     }    
  35.         
  36.     
  37.     /*  
  38.      * 获取程序的名字   
  39.      */    
  40.     public String getAppName(String packname){    
  41.           try {    
  42.                  ApplicationInfo info = pm.getApplicationInfo(packname, 0);     
  43.                  return info.loadLabel(pm).toString();    
  44.             } catch (NameNotFoundException e) {    
  45.                 // TODO Auto-generated catch block    
  46.                 e.printStackTrace();    
  47.                  
  48.             }    
  49.     }    
  50.     /*  
  51.      * 获取程序的权限  
  52.      */    
  53.     public String[] getAppPremission(String packname){    
  54.           try {    
  55.               PackageInfo packinfo =    pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);    
  56.               //获取到所有的权限     
  57.               return packinfo.requestedPermissions;    
  58.     
  59.             } catch (NameNotFoundException e) {    
  60.                 e.printStackTrace();    
  61.                    
  62.             }    
  63.     }    
  64.         
  65.         
  66.     /*  
  67.      * 获取程序的签名   
  68.      */    
  69.     public String getAppSignature(String packname){    
  70.           try {    
  71.               PackageInfo packinfo =    pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);    
  72.               //获取到所有的权限     
  73.               return packinfo.signatures[0].toCharsString();    
  74.     
  75.             } catch (NameNotFoundException e) {    
  76.                 e.printStackTrace();    
  77.                   
  78.             }    
  79.     }    
转载地址:http://blog.csdn.net/yudajun/article/details/7855270



阅读更多
个人分类: Android 基础
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭