Getting android application version programatically

Here we go:

private String getVersionName(Context context,Class class_name)
            PackageInfo pinfo;
            ComponentName comp = new ComponentName(context,class_name);
            pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
            return pinfo.versionName;
        catch (Exception e)
            return null;

Happy coding:-)


  1. @Javin: If my sharing is usefull to you, then I'm happy:-)

    Well, with respect to above post: Its a good practice to return any other relevant values other than "null". I have read this suggestion in one of blog post(not sure but I suppose from Dzone). So please correct the same while implementing..