suppender of AndroidStudy
12/16/2016 - 1:32 PM

app相关辅助类

app相关辅助类

/** 
 * app相关辅助类 
 */
public class AppUtil { 
    private AppUtil() { 
        /* cannot be instantiated*/
        throw new UnsupportedOperationException("cannot be instantiated");
    } 
  
    /** 
     * 获取应用程序名称 
     * 
     * @param context 
     * @return 
     */
    public static String getAppName(Context context) {
  
        PackageManager packageManager = context.getPackageManager();
        try { 
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } 
        return null; 
    } 
  
    /** 
     * 获取应用程序版本名称信息 
     * 
     * @param context 
     * @return 当前应用的版本名称 
     */
    public static String getVersionName(Context context) {
        try { 
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } 
        return null; 
    } 
  
    /** 
     * 获取应用程序的版本Code信息 
     * @param context 
     * @return 版本code 
     */
    public static int getVersionCode(Context context) {
        try { 
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } 
        return 0; 
    } 
}