Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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());
    }

    public static String getVersionName(Context context, String pkgName) {
        return getPackageInfo(context, pkgName).versionName;
    }

    private static PackageInfo getPackageInfo(Context context, String pkgName) {
        PackageInfo packageInfo = null;
        try {
            PackageManager pm = context.getPackageManager();
            packageInfo = pm.getPackageInfo(pkgName, 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            packageInfo = new PackageInfo();
            packageInfo.versionCode = 1;
            packageInfo.versionName = "1.0";
        }
        return packageInfo;
    }
}