Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.content.pm.PackageManager;

import android.util.Log;

public class Main {
    /**
     * Returns the version name of the current app
     *
     * @param context an app context to retrieve the package manager and package name
     * @return the version name or "unknown" if the package name (of this current app) is not found
     */
    public static String getVersionName(Context context) {
        String versionName = "unknown";
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(),
                    PackageManager.GET_ACTIVITIES).versionName;
        } catch (PackageManager.NameNotFoundException ignore) {
            Log.d("NoTils", "Version name not found, Paul has a hat to eat.", ignore);
        }
        return versionName;
    }
}