get Current Version Name - Android Android OS

Android examples for Android OS:OS Version

Description

get Current Version Name

Demo Code


//package com.java2s;

import android.content.Context;

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

public class Main {
    public static String getCurrentVersionName(Context context) {
        String currentVersionName = "";

        try {//from   w  ww  .  j a v  a2  s . co  m
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            currentVersionName = pi.versionName;
            if (currentVersionName == null
                    || currentVersionName.length() <= 0) {
                currentVersionName = "1.0.0";
            }
        } catch (Exception e) {
            currentVersionName = "1.0.0";
        }

        return currentVersionName;
    }
}

Related Tutorials