Android 开发工具类 01_AppUtils

1、获取应用程序名称;

2、获取应用程序版本信息。

 1 import android.content.Context;
 2 import android.content.pm.PackageInfo;
 3 import android.content.pm.PackageManager;
 4 import android.content.pm.PackageManager.NameNotFoundException;
 5 
 6 // 跟 App 相关的辅助类
 7 public class AppUtils
 8 {
 9 
10     private AppUtils()
11     {
12         /* cannot be instantiated */
13         throw new UnsupportedOperationException("cannot be instantiated");
14 
15     }
16 
17     /**
18      * 获取应用程序名称
19      */
20     public static String getAppName(Context context)
21     {
22         try
23         {
24             PackageManager packageManager = context.getPackageManager();
25             PackageInfo packageInfo = packageManager.getPackageInfo(
26                     context.getPackageName(), 0);
27             int labelRes = packageInfo.applicationInfo.labelRes;
28             return context.getResources().getString(labelRes);
29         } catch (NameNotFoundException e)
30         {
31             e.printStackTrace();
32         }
33         return null;
34     }
35 
36     /**
37      * [获取应用程序版本名称信息]
38      * 
39      * @param context
40      * @return 当前应用的版本名称
41      */
42     public static String getVersionName(Context context)
43     {
44         try
45         {
46             PackageManager packageManager = context.getPackageManager();
47             PackageInfo packageInfo = packageManager.getPackageInfo(
48                     context.getPackageName(), 0);
49             return packageInfo.versionName;
50 
51         } catch (NameNotFoundException e)
52         {
53             e.printStackTrace();
54         }
55         return null;
56     }
57 
58 }

 

posted @ 2015-05-28 13:01  壬子木  阅读(240)  评论(0编辑  收藏  举报