最近老是用到關于程式包名,圖示問題,現總結如下:
[java] view plain copy
- PackageInfo info;
- try {
- info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
- // 目前應用的版本名稱
- String versionName = info.versionName;
- // 目前版本的版本号
- int versionCode = info.versionCode;
- // 目前版本的包名
- String packageNames = info.packageName;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
另一種方法:
[java] view plain copy
- public class Info {
- Context context ;
- PackageManager pm ;
- public Info(Context context) {
- this.context = context;
- pm = context.getPackageManager();
- }
- public Drawable getAppIcon(String packname){
- try {
- ApplicationInfo info = pm.getApplicationInfo(packname, 0);
- return info.loadIcon(pm);
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String getAppVersion(String packname){
- try {
- PackageInfo packinfo = pm.getPackageInfo(packname, 0);
- return packinfo.versionName;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- public String getAppName(String packname){
- try {
- ApplicationInfo info = pm.getApplicationInfo(packname, 0);
- return info.loadLabel(pm).toString();
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String[] getAppPremission(String packname){
- try {
- PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
- //擷取到所有的權限
- return packinfo.requestedPermissions;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- public String getAppSignature(String packname){
- try {
- PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);
- //擷取到所有的權限
- return packinfo.signatures[0].toCharsString();
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- http://blog.csdn.net/yudajun/article/details/7855270