Get version name from Context - Android App

Android examples for App:Package

Description

Get version name from Context

Demo Code


//package com.java2s;

import android.content.Context;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class Main {

    public static String getVersionName(Context context) {
        return getVersionName(context, context.getPackageName());
    }//from  w ww  . j  ava2  s.co  m

    public static String getVersionName(Context context, String packageName) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
            if (null != packageInfo) {
                versionName = packageInfo.versionName;
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static String getPackageName(Context context) {
        return context.getPackageName();
    }
}

Related Tutorials