Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

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

public class Main {
    /**
     * Get the App versioncode
     * @param context
     * @return if return == -1 means errors happen
     */
    public static int getAppVersionCode(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(getAppPackageName(context), 0);
            return info.versionCode;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * Get the App PackageName
     * @param context
     * @return packeagename
     */
    public static String getAppPackageName(Context context) {
        return context.getPackageName();
    }
}